Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate ng-template in separate file

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.

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

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!
...
....
like image 218
Sudhakar Avatar asked Oct 12 '15 06:10

Sudhakar


People also ask

Can ng-template be nested?

Similarly if the child <ng-template> is nested inside a parent <ng-template>, even then the child can easily access all the data passed to the parent <ng-template>. You can check the entire working example below.

Where do you put ng templates?

ng-template should be used along with structural directives like [ngIf],[ngFor],[NgSwitch] or custom structural directives. That is why in the above example the contents of ng-template are not displayed. ng-template never meant to be used like other HTML elements.

Can we use ng-template inside ng-template?

ng-template is an Angular element that is used for rendering HTML in a template. However, it is not rendered directly on DOM. If you include an ng-template tag to a template, the tag and the content inside it will be replaced by comment upon render.

Is ng-template a div?

To sum up, ng-content is used to display children in a template, ng-container is used as a non-rendered container to avoid having to add a span or a div, and ng-template allows you to group some content that is not rendered directly but can be used in other places of your template or you code.


1 Answers

You don't need ng-include.

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

<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-->
like image 177
Tasnim Reza Avatar answered Nov 16 '22 00:11

Tasnim Reza