My Technical Notes

Wednesday, 17 June 2015

Angular: using a template and including it

Given a template:


<script type="text/ng-template" id="myTemplate">
    
My value is {{name}}
</script>

We can include it using the following statement:


<div ng-include="'myTemplate'"></div>

Notice the single quotes around `myTemplate` in the include statement? - If they were not there, angular would treat it like a variable instead of a string.

Including an external template

To include an HTML template defined at `/path/to/template.html`:


<div>
    My value is {{name}}
</div>

we specify the path in the `ng-include` attribute (notice the single quotes again):


<div ng-include="'/path/to/template.html'"></div>

No comments: