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

时间:2021-12-02 12:05:35

I'm using angularUI typehead for the input element. I'm using custom template to show the values. Its all working fine. My question is how do i separate the ng-template into a separate file so that it can be re-used in other files as well.

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

In case my words are not clear, please note that I'm referring to ng-templates specifically and not concerned about separate other html content

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

Here's the code:

这是代码:

 // 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>

Putting it in a partial did not work, e.g: <div ng-include="app/client/bug/new/my-ng-template.partial.html"></div> throws:

将其置于部分不起作用,例如:

抛出:

Tried to load angular more than once.
 [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
...
....

1 个解决方案

#1


11  

You don't need ng-include.

您不需要ng-include。

Move the template into separate file, lets say the file name customTemplate.html and file contents like

将模板移动到单独的文件中,让我们说文件名customTemplate.html和文件内容如

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

Don't include <script type="text/ng-template" id="customTemplate.html"> tag when it is moved into separate file.

Use the correct file path like

使用正确的文件路径,如

<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-->

#1


11  

You don't need ng-include.

您不需要ng-include。

Move the template into separate file, lets say the file name customTemplate.html and file contents like

将模板移动到单独的文件中,让我们说文件名customTemplate.html和文件内容如

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

Don't include <script type="text/ng-template" id="customTemplate.html"> tag when it is moved into separate file.

Use the correct file path like

使用正确的文件路径,如

<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-->