0

我的 DOM 中有一个脚本标签,其中包含如下模板:

<body>

   <div ng-insert='ui'></div>  <!--inject ui here-->

   <script id='ui' type='text/html'>
      <div ng-model='name'></div>
   </script>
</body>

...并且我希望使用我在上面编写的 ng-insert 之类的指令将其添加到 DOM 中。

我还没有找到一个内置的来做到这一点。ng-include 似乎只加载到远程模板中。我认为这将是一个相当简单的指令,我可以尝试,但我想确保我不会重新发明已经存在的东西。

4

2 回答 2

2

ng-include可以与标签type="text/ng-template"上使用的预定义模板结合使用。script

<script type="text/ng-template" id="templateId.html">
  This is the content of the template
</script>

注意:script包含模板的标签不需要包含在文档的头部,但必须在ng-app定义的下方。

稍后使用它来获取模板:

<div ng-include="'templateId.html'"></div>

注意:模板的名称用单引号括起来,作为一个字符串。

您还可以通过 JavaScript 获取此模板:

$templateCache.get('templateId.html')

参考文献:

于 2013-10-03T14:29:20.583 回答
1

我认为这就是您正在寻找 ng-includehttp://jsfiddle.net/mrajcok/MfHa6/script type="text/ng-template"

于 2013-10-03T14:23:03.173 回答