Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding AngularJS Templates

  Author : Shailendra Chauhan
Posted On : 15 Sep 2014
Total Views : 12,734   
Updated On : 15 Sep 2014
 

In Angular, templates are the views with the HTML enriched by Angular elements like directive and attributes. Templates are used to display the information from the model and controller that a user sees in his browser. An angular templates can have Directive, HTML markup, CSS, Filters, Expressions and Form controls.

A simple Angular Template

<html ng-app>
<body ng-controller="MyController">
 <input ng-model="name" value="dotnet-tricks.com">
 <br/>
 Input Value is : {{name}}
 <button ng-click="changeValue()">Click me!</button>
 <script src="angular.js">
 </body>
</html>

Types of Templates

There are two types of templates :

  1. Static Templates

    A static template is defined by using script tag. It must has an id attribute with a unique value and a type attribute with value text/ng-template. Also, a static template must be inside the scope of the ng-app directive otherwise it will be ignored by Angular.

     <script type="text/ng-template" id="person.html">
     {{person.name}} : {{person.address}}
     </script>
    

    A static template can be rendered by using ng-include directive.

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

    Full Example of Static Template

    <!DOCTYPE html>
    <html>
    <head>
     <title>AngularJS Static Templates</title>
     <script src="lib/angular.js"></script>
     <script>
     //defining module
     var app = angular.module('app', []);
    
     app.controller("myController", function ($scope) {
     $scope.person = { name: "Deepak Chauhan", address: "Delhi" };
     });
    
     app.controller("homeController", function ($scope) {
     $scope.persons = [{ name: "Deepak Chauhan", address: "Delhi" }, { name: "Shailendra Chauhan", address: "Noida" }, { name: "Kuldeep Chauhan", address: "Gurgaon" }]
     });
    
     </script>
    
    </head>
    <body ng-app="app">
     <h1>AngularJS : Static Templates</h1>
     <!--It should be the part of ng-app directive-->
     <script type="text/ng-template" id="person.html">
     {{person.name}} : {{person.address}}
     </script>
    
     <div ng-controller="myController">
     <h1>myController</h1>
     <!--Please not the single quotes around person.html. The value of the ng-include is an expression and person.html is a string value, so put single quotes around it.-->
     <div ng-include="'person.html'"></div>
     </div>
    
     <div ng-controller="homeController">
     <h1>homeController</h1>
     <div ng-repeat="person in persons" ng-include="'person.html'"></div>
     </div>
    </body>
    </html>
    
  2. Dynamic Templates

    A dynamic template is an html page which is compiled and rendered by Angular on demand. The above static template can be created as a HTML page within templates folder of your app like as:

    person.html

    {{person.name}} : {{person.address}}

    A dynamic template can be rendered by using ng-include directive.

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

    Full Example of Dynamic Template

    <!DOCTYPE html>
    <html>
    <head>
     <title>AngularJS Dynamic Templates</title>
     <script src="lib/angular.js"></script>
     <script>
     //defining module
     var app = angular.module('app', []);
    
     app.controller("myController", function ($scope) {
     $scope.person = { name: "Deepak Chauhan", address: "Delhi" };
     });
    
     app.controller("homeController", function ($scope) {
     $scope.persons = [{ name: "Deepak Chauhan", address: "Delhi" }, { name: "Shailendra Chauhan", address: "Noida" }, { name: "Kuldeep Chauhan", address: "Gurgaon" }]
     });
    
     </script>
    
    </head>
    <body ng-app="app">
     <h1>AngularJS : Dynamic Templates</h1>
     <div ng-controller="myController">
     <h1>myController</h1>
     <!--Please not the single quotes around person.html. The value of the ng-include is an expression and person.html is a string value, so put single quotes around it.-->
     <div ng-include="'templates/person.html'"></div>
     </div>
    
     <div ng-controller="homeController">
     <h1>homeController</h1>
     <div ng-repeat="person in persons" ng-include="'templates/person.html'"></div>
     </div>
    </body>
    </html>
    

    person.html

     {{person.name}} : {{person.address}}

Note

Always put the single quotes around the name of template with ng-include directive, since it accept an expression. Hence to indicate to Angular that template (person.html) is a string value you have to put single quotes around it's name.

What do you think?

I hope you will enjoy the AngularJS Templates while developing your app with AngularJS. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS (0)
11 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

Know More
6 MAR
Master Class Fast Track MVC 5 with Angular2 Development (Online)

10:30 AM -03:30 PM IST (Fast Track)

Know More
6 MAR
Angular 2 Master Class Fast Track Online Development Training (Online)

10:30 AM- 01:30 PM IST

Know More
27 FEB
ASP.NET MVC with Angular2 Development (Online)

07:00 AM - 09:00 AM IST( MON, WED, FRI)

Know More
25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

05:00 PM-06:30 PM

Know More
25 FEB
Xamarin Forms : Build Cross-platform Apps (Classroom)

3:00 PM-4:30 PM IST

Know More
25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

3:00 PM-4:30 PM

Know More
14 FEB
.NET Development (Classroom)

11:00 AM-12:00 PM

31 JAN
ASP.NET MVC with Angular2 Development (Online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

30 JAN
NODEJS & MEAN Stack 2.x Development (Online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

15 JAN
PPC Marketing (Classroom)

04:00 PM-05:30 PM

10 JAN
ASP.NET MVC with AngularJS Development (Online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

31 DEC
ASP.NET MVC with AngularJS Development (Classroom)

09:30 AM-11:00 AM

5 NOV
ASP.NET MVC with AngularJS Development (Classroom)

08:00 AM-09:30 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 

Professional Speaks

+