官术网_书友最值得收藏!

Loading external templates for best practices

Thinking in terms of best practices, let's see how to use the same modal directive with an external template, using the templateUrl property instead of the template property. Before we go further, let's explore the two ways to use templates.

Use the script tag of ng-template, as shown in the following example:

<body ng-app='SimpleModal'>
  <script type="text/ng-template" id="modal.html">
  <div ng-show='show'>
    <div class='modal-overlay' ng-click='hideModal()'></div>
    <div class='modal-background' ng-style='windowStyle'>
      <div class='modal-close' ng-click='hideModal()'>X</div>
      <div class='modal-content' ng-transclude></div>
    </div>
  </div>
  </script>
</body>

Alternatively, place the HTML content in a separate file; in this case, the template will be an external file, not just external from the directive code. The code is as follows:

<body ng-app='SimpleModal'>
  <div ng-controller='ModalCtrl'>
    <button ng-click='toggleModal()'>Open Modal</button>
    <modal-window show='modalShown' width='400px' height='60%'>
      <p>Hello Simple Modal Window with External Template<p>
    </modal-window>
  </div>
</body>

Both ways have the same result, and we will see the difference later. For now, let's focus on the HTML template.

Getting ready

For this recipe, we will be using the same code base as the previous recipe.

How to do it…

  1. Let's replace the entire template string with the following code:
    // loading external templates
    app = angular.module('SimpleModal', []);
    
    app.directive('modalWindow', function() {
      return {
        restrict: 'E',
        scope: {
          show: '='
        },
        replace: true, // Replace with template
        transclude: true, // To use custom content
        link: function(scope, element, attrs) {
    
          scope.windowStyle = {};
    
          if (attrs.width) {
            scope.windowStyle.width = attrs.width;
          }
          if (attrs.height) {
            scope.windowStyle.height = attrs.height;
          }
    
          scope.hideModal = function() {
            scope.show = false;
          };
        },
        templateUrl: "modal.html"
      };
    });
  2. Remember that we keep the same controller code as the previous example. The templateUrl property points to an external file, so place the following code in a blank HTML file and save it as modal.html:
    <body ng-app='SimpleModal'>
      <div ng-controller='ModalCtrl'>
        <button ng-click='toggleModal()'>Open Modal</button>
        <modal-window show='modalShown' width='400px' height='60%'>
          <p>Hello Simple Modal Window with External Template<p>
        </modal-window>
      </div>
    </body> 

How it works…

With the templateUrl property, we can load an external HTML template inside our current HTML file. It is very useful to use this practice because we can reuse the same template in different places in the application. We will cover this topic later on in this book.

Tip

To load external templates inside your files, you must have a HTTP server.

There's more…

When we use type=text/ng-template with the script tag, we need to place the modal content inside our page, and the content will be hidden by the browser. The script tag is used to tell the browser that there is a code snippet, usually in JavaScript. In this way, the content of the tag is interpreted differently by the browser. In our case, the type attribute indicates that we have a template, as we can see in the previous example.

We can use the same example, as shown in the following code:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.x/angular.js"></script>
<title>Modal Window Directive</title>
<style>
...
</style>
</head>
<body ng-app='SimpleModal'>
  <script type="text/ng-template" id="modal.html">
    <div ng-controller='ModalCtrl'>
      <button ng-click='toggleModal()'>Open Modal</button>
      <modal-window show='modalShown' width='400px' height='60%'>
        <p>Hello Simple Modal Window using ng-template<p>
      </modal-window>
    </div>
  </script>
</body>
</html>

See also

主站蜘蛛池模板: 芦溪县| 广丰县| 雅江县| 广东省| 鄂州市| 固安县| 丰原市| 类乌齐县| 泾源县| 彭山县| 且末县| 莲花县| 屯门区| 兴海县| 万山特区| 太和县| 吴桥县| 宣恩县| 靖宇县| 临高县| 怀柔区| 灌南县| 大丰市| 长沙县| 江口县| 富锦市| 漯河市| 汝南县| 堆龙德庆县| 开原市| 永修县| 保康县| 灵宝市| 高青县| 津市市| 盐源县| 张掖市| 青田县| 阿巴嘎旗| 平湖市| 建平县|