angularjs - 如何在单独的文件中分离 ng-template

标签 angularjs angular-ui-bootstrap angularjs-ng-template

我正在使用 angularUI typehead对于输入元素。我正在使用自定义模板来显示值。它一切正常。我的问题是如何分离 ng-template到一个单独的文件中,以便它也可以在其他文件中重复使用。

如果我的话不清楚,请注意我指的是ng-templates特别不关心单独的其他 html 内容

这是代码:

 // custom template begin 
 // this is the ng-template
 // I'd like to move this custom template into to another file 
 // similar to partials
 <script type="text/ng-template" id="customTemplate.html">
    <a>
        <b>{{match.model.name}}</b>
        <div>{{match.model.username}}</div>
    </a>
</script>
 //custom template end


// input element makes use of the ng-template defined above
<div class="form-group">
<label class="col-md-2 control-label normal" for="assignTo">Assign To</label>
 <div class="col-md-3">
    <input name="bug_assignTo" id="bug_assignTo" ng-model="bug.assignTo" typeahead="user.username as user.name for user in config.users | filter:$viewValue | limitTo:8" class="form-control input-sm" typeahead-on-select="bug.assignTo = $item" placeholder="type name" typeahead-template-url="customTemplate.html"></input>
 </div>

将它放在部分中不起作用,例如:<div ng-include="app/client/bug/new/my-ng-template.partial.html"></div>抛出:
Tried to load angular more than once.
 [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
...
....

最佳答案

你不需要ng-include .

将模板移动到单独的文件中,假设文件名 customTemplate.html和文件内容如

<a>
    <b>{{match.model.name}}</b>
    <div>{{match.model.username}}</div>
</a>

不包括 <script type="text/ng-template" id="customTemplate.html">当它被移动到单独的文件中时标记。

使用正确的文件路径,如
<input name="bug_assignTo" 
               id="bug_assignTo" 
               ng-model="bug.assignTo" 
               typeahead="user.username as user.name for user in config.users | filter:$viewValue | limitTo:8" 
               class="form-control input-sm" 
               typeahead-on-select="bug.assignTo = $item" 
               placeholder="type name" 
               typeahead-template-url="customTemplate.html" /> <!--put correct physical path here, if it is inside partial folder then use partial/customTemplate.html-->

关于angularjs - 如何在单独的文件中分离 ng-template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33074883/

相关文章:

javascript - Meanjs 禁止错误

javascript - 在 AngularJS 中将商品添加到购物车

javascript - 将数据从父指令传递到子指令

javascript - 大型 AngularJS 应用程序的页面加载性能问题

angularjs - 在 ng Select 中设置默认值

angularjs - 使用动态内容的 Angular Bootstrap-UI 轮播

javascript - Moment js 时间选择器

html - 我怎样才能通过使用 Bootstrap 网格来实现它?

javascript - `ng-template` 未找到内联 `ng-include`