ng-template作为空字符串添加到$ templateCache

时间:2014-10-31 17:31:09

标签: javascript angularjs

我有以下html

<div ng-app="theapp" id="ng-app">

    <!-- templates -->
    <script type="text/ng-template" src="https://app.com/templates/about.html" id="templates/about.html"></script>

    <ion-nav-view></ion-nav-view>
</div>

我希望'templates/about.html'位于$templateCache ...并且它是......(不是undefined)...但该值为空字符串。我正在我的角度模块控制器的Chrome调试器中检查这个。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

<强> 1)即可。您无法使用脚本type="text/ng-template"指向外部网址模板文件。当Angular看到具有自定义模板类型的此类脚本标记时,它将使用其内部文本内容放入模板缓存中。 Angular不会下载任何内容。

作为参考,我可以在这里放置scriptDirective的编译功能:

compile: 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);
  }
}

正如您所看到的,在您的案例中最终使用空字符串模板并不奇怪:您的src属性从未使用过,脚本文本确实是空的。

<强> 2)即可。虽然我不认为这是一个非常好的想法,但是可以创建一个script指令(因为你可以有许多同名的指令),这将是尊重{{1并为您下载模板:

src

演示:http://plnkr.co/edit/TChPjDr9iAq4Zi55N0rq?p=preview

相关问题