In this article, we will learn about Directives in
AngularJS application. Before starting, I suggest you read my previous articles in this series.
Directives are one of the core features of AngularJS. Directives allow us to extend HTML with new attributes, elements, and classes. Directives are used for DOM Manipulation, Data Binding, Loading View, Events firing, etc. We can define directives in multiple ways:
Some of the directives like ng-view etc. can be used as custom element.
In this Article, we will see some built in AngularJS directives. In this series, we have already seen some of the Directives like ng-app, ng-model, ng-controller, ng-bind etc. so, I am not going to cover them again.
1. ng-init: ng-init is used to initialize special properties for ng-repeat directive and for injecting data via server side scripting language in AngularJS applications. It is recommended to use controller for initializing a property/model in AngularJS application.
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <script src="Script/angular.js"></script>
- </head>
- <body ng-app>
-
- <div ng-init="Name='Anoop'">
- <h3>ng-init Example</h3>
- Name: <input type="text" ng-model="Name"/>
- </div>
- </body>
- </html>
Preview:
2. ng-repeat: ng-repeat directive repeats html elements for each item in a collection. It is much similar to foreach loop in C# or other programming language.
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Directive Demo</title>
- <script src="Script/angular.js"></script>
- </head>
- <body ng-app>
-
- <div ng-init="Names=['Anoop','Akshay','Ajay','Abhishek','Arjun']">
- <h3>ng-repeat Example</h3>
- <ul>
-
- <li ng-repeat="name in Names">{{name}}</li>
- </ul>
- </div>
- </body>
- </html>
Preview:
Example: Fetch data from Array of Objects using ng-repeat.
- <!DOCTYPE html>
- <html>
- <head>
- <title>Directive Demo</title>
- <script src="Script/angular.js"></script>
- </head>
- <body ng-app>
-
- <div ng-init="Employees=[
- { Name: 'Anoop', City: 'New Delhi' },
- { Name: 'Akshay', City: 'Mumbai' },
- { Name: 'Ajay', City: 'Punjab' },
- { Name: 'Arjun', City: 'Haryana' }
- ]">
- <h3>ng-repeat Example</h3>
- <table border="1" style="border-collapse:collapse">
- <tr><th>Name</th><th>City</th></tr>
-
- <tr ng-repeat="Employee in Employees"><td>{{Employee.Name}}</td><td>{{Employee.City}}</td></tr>
- </table>
- </div>
- </body>
- </html>
Preview:
3. ng-show/ng-hide Directive: ng-show/ng-hide directive allow us to show or hide different HTML elements based on the expression passed to that directive.
Example of ng-show directive:
- <!DOCTYPE html>
- <html>
- <head>
- <title>ngshow Demo</title>
- <script src="Script/angular.js"></script>
- </head>
- <body ng-app>
- <h3>ng-show Example</h3>
-
- <input type="checkbox" ng-model="isVisible" /> Show Div
-
- <div ng-show="isVisible">
- Name: <input type="text" />
- </div>
- </body>
- </html>
Preview:
Example of ng-hide directive:
- <!DOCTYPE html>
- <html>
- <head>
- <title>ng-hide Demo</title>
- <script src="Script/angular.js"></script>
- </head>
- <body ng-app>
- <h3>ng-hide Example</h3>
-
- <input type="checkbox" ng-model="isHidden" /> Hide Div
-
- <div ng-hide="isHidden">
- Name: <input type="text" />
- </div>
- </body>
- </html>
Preview:
4. ng-Click: ng-click directive updates the model/property of AngularJS application when an element is clicked.
- <!DOCTYPE html>
- <html>
- <head>
- <title>ngClick Demo</title>
- <script src="Script/angular.js"></script>
- </head>
- <body ng-app>
-
- <div ng-init="count=0">
- <h3>ng-click Example</h3>
-
- <button ng-click="count=count+1">Increase Value by 1</button> <button ng-click="count=count-1">Decrease Value by 1</button>
- <p>Count={{count}}</p>
- </div>
- </body>
- </html>
Preview:
Please
visit for reading more about various built in directives.
Hope you liked it. Thanks!
Read more articles on AngularJS: