javascript - AngularJs's templateUrl : Url or ID? - Stack Overflow

AngularJS docs provide a templateUrl example : using :ngApp...<div ng-controller="Ctrl"&

AngularJS docs provide a templateUrl example :

using :

  //ngApp...
 <div ng-controller="Ctrl">
      <div my-customer></div>
    </div>

And in the controller :

....directive('myCustomer', function() {
    return {
      templateUrl: 'my-customer.html'
    };
  })

Where my-customer.html is an external file :

All ok. but they provide the option to edit it (via jsfiddle) :

But there- it is as a template tag ! :

<div ng-app="docsTemplateUrlDirective">
  <div ng-controller="Ctrl">
    <div my-customer></div>
  </div>



      <!-- my-customer.html -->
      <script type="text/ng-template" id="my-customer.html">
        Name: {{customer.name}} Address: {{customer.address}}
      </script>
    </div>

Question :

How does NG knows if it's an external file or is it an Tag element ID ? and how can I force it to a certain type ?

(p.s. - didn't find it in the docs).

AngularJS docs provide a templateUrl example :

using :

  //ngApp...
 <div ng-controller="Ctrl">
      <div my-customer></div>
    </div>

And in the controller :

....directive('myCustomer', function() {
    return {
      templateUrl: 'my-customer.html'
    };
  })

Where my-customer.html is an external file :

All ok. but they provide the option to edit it (via jsfiddle) :

But there- it is as a template tag ! :

<div ng-app="docsTemplateUrlDirective">
  <div ng-controller="Ctrl">
    <div my-customer></div>
  </div>



      <!-- my-customer.html -->
      <script type="text/ng-template" id="my-customer.html">
        Name: {{customer.name}} Address: {{customer.address}}
      </script>
    </div>

Question :

How does NG knows if it's an external file or is it an Tag element ID ? and how can I force it to a certain type ?

(p.s. - didn't find it in the docs).

Share Improve this question asked Jan 30, 2014 at 8:59 Royi NamirRoyi Namir 149k144 gold badges493 silver badges831 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I will add to what @QuarK started with, perhaps filling in a bit more detail.

The $templateCache is used as a repository for templates once they have been resolved (inline or from a file).

The source of the script tag with text/ng-templates looks like this:

var scriptDirective = ['$templateCache', function($templateCache) {
  return {
    restrict: 'E',
    terminal: true,
    pile: function(element, attr) {
      if (attr.type == 'text/ng-template') {
        var templateUrl = attr.id,
            // IE is not consistent, in scripts we have to read .text but in other nodes we have to read .textContent
            text = element[0].text;

        $templateCache.put(templateUrl, text);
      }
    }
  };
}];

You can see that if a script tag is found, and the attr.type is text/ng-template, then angular puts the content into the $templateCache indexed by the templateUrl.

Using script tags like this has only been introduced recently to Angular. So, this is an option that allows an inline definition without requiring an external file. However, underneath, once resolved, both use the same mechanism to be read and piled ($templateCache).

Hope this helps.

This is through $templateCache, you can find the documentation in it's API:

http://docs.angularjs/api/ng.$templateCache

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745206482a4616617.html

相关推荐

  • javascript - AngularJs&#39;s templateUrl : Url or ID? - Stack Overflow

    AngularJS docs provide a templateUrl example : using :ngApp...<div ng-controller="Ctrl"&

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信