What are templates in AngularJS ?
Last Updated :
12 Jul, 2025
Templates in AngularJS are simply HTML files filled or enriched with AngularJS stuff like attributes and directives. A directive is a marker element that is used to target a particular attribute or class to render its behavior according to the needs. Model and controller in Angular are combined with the templates to manipulate the view the user sees in his/her browser. Angular templates can also accommodate CSS, Form controls, Filters, and Expressions. There are two types of templates:
- Static Template
- Dynamic Templates
Below are examples illustrating both templates.
Static Template: A static template is defined by using a script tag. An id and type attribute with value text/ng-template must be provided to it for the static template to work. Also, it should be noted that a static template will work only if it is under the ng-app scope, otherwise, it will be ignored by Angular. A static template can be rendered by using the ng-include directive.
For example:
<div ng-include="'geeksforgeeks.html'"></div>
Example 1: This example demonstrates the simple static template in AngularJS.
HTML
<!DOCTYPE html>
<html ng-app>
<head>
<script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js">
</script>
<title>Angular Static Template</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
}
h1 {
color: green;
}
</style>
</head>
<body ng-controller="GeekController">
<h1>GeeksforGeeks</h1>
<h3>Angular Static Template</h3>
<br> Input Value is:
<input ng-model="geek" value="Your Value Here">
<button ng-click="gfg()">Button</button>
<script src="angular.js"></script>
</body>
</html>
Output:
Dynamic Templates: Just like the name suggests, the dynamic templates are used to work with the runtime environments. It is compiled and rendered by Angular on user demand. A dynamic template can be rendered by using the ng-include directive.
For example:
<div ng-include="'templates/geeksforgeeks.html'"></div>
Example 2: This example demonstrates the Dynamic Template in AngularJS.
HTML
<html>
<head>
<title>Angular Dynamic Template</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js">
</script>
<style>
body {
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
h1 {
color: green;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h3>Angular Dynamic Template</h3>
<div ng-app="gfg">
<p>
<a href="#addCourse">Add Course</a>
</p>
<p>
<a href="#viewCourses">View Courses</a>
</p>
<div ng-view></div>
<script type="text/ng-template"
id="addCourse.htm">
<h2> Add Course </h2> {{message}}
</script>
<script type="text/ng-template"
id="viewCourses.htm">
<h2> View Courses </h2> {{message}}
</script>
</div>
<script>
var gfg = angular.module("gfg", ['ngRoute']);
gfg.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/addCourse', {
templateUrl: 'addCourse.htm',
controller: 'AddCourseController'
}).when('/viewCourses', {
templateUrl: 'viewCourses.htm',
controller: 'ViewCoursesController'
}).otherwise({
redirectTo: '/addCourse'
});
}]);
gfg.controller('AddCourseController', function($scope) {
$scope.message =
"This page will be used to display add Course";
});
gfg.controller('ViewCoursesController', function($scope) {
$scope.message =
"This page will be used to display all the Courses";
});
</script>
</body>
</html>
Output:
Similar Reads
What are Directives in AngularJS ? AngularJS directives are extended HTML attributes having the prefix ng-. Directives are markers on the DOM element which tell Angular JS to attach a specified behavior to that DOM element or even transform the DOM element with its children. During compilation, the HTML compiler traverses the DOM mat
7 min read
Angular PrimeNG Carousel Templates Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. It provides a lot of templates, components, theme design, an extensive icon library, and much more.
4 min read
Angular PrimeNG Panel Templates Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use the Panel Templates in Angular PrimeNG. The Panel Component all
4 min read
AngularJS $templateRequest Service The AngularJS $templateRequest service is used to fetch the contents of an HTML template. It makes an HTTP GET request to the specified template URL and returns the template content as a string. The $templateRequest service is part of the ngRoute module, which must be included in your application in
3 min read
Angular PrimeNG Galleria Templates Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. It provides a lot of templates, components, theme design, an extensive icon library, and much more.
4 min read
What is Angular Material? User Experience is one of the most important things in web development. Angular Material emerges as a powerful tool for developers, offering numerous UI components designed to elevate your Angular applications to new heights of elegance and functionality. In this article, we'll learn more about Angu
4 min read