Directives extend your HTML tags by attaching with them. Directives attach specific behavior with HTML tag. AngularJS directives start with ng- prefix. AngularJS has a set of many built-in directives for many different functionalities. We also can create our own custom directive in AngularJS.
Directive reference from Angular.org, check here: https://docs.angularjs.org/guide/directive
In our demo application, we have already used some of the directives.
- ng-app directive - this is the first thing we add in our AngularJS application. It initializes our AngularJS application. ng-app directive defines the root element for the AngularJS application.
- ng-init directive - It Initializes your application data. Ng-init sets the initial data or values for your application.
In our demo application, we have used ng-init to assign some value to our Firstapp variable.
- <body ng-app="" ng-init="Firstapp='Hello fisrt angularjs application'">
- ng-model directive - It binds the value of HTML controls to the application data. Ng-model bind AngularJS application data to HTML tags. It uses ng-model with some variable name that variable. That variable holds data to bind with HTML.
In our Demo application, we have used,
- <p>First Name: <input type="text" ng-model="firstName"></p>
- <p>Last Name: <input type="text" ng-model="lastName"></p>
String expression -
- <span >full Name: {{ firstName + " " + lastName }} </span>
ng-model="firstName" is holding value for firstName and letter, displaying that firstName value. Ng-model provide two-way data binding. We will look model letter in detail.
ng-bind directive binds the AngularJS data to the HTML element with the value of a given variable, or expression. It behaves quite same as ng-model but difference is ng-bind provide one-way data binding.
ng-click directive raises the click event on the HTML control.
ng-Repeat directive is used for looping logic. Every programming language has loops like for loop, foreach loop. AngularJS has ng-repeat. It repeats HTML elements and clones the elements once for each item. If it is attached to the <tr> tag, then it will create many <tr> tags till loop ends.
Example
- <table border="1" ng-init="fullNames=[{firstName:'Arvind',lastName:'Singh'},{firstName:'Risha',lastName:'Baghel'},
-
- {firstName:'Janvi',lastName:'Baghel'}]">
- <tr>
- <td>First Name</td>
- <td>Last Name</td>
- </tr>
- <tr ng-repeat="name in fullNames">
- <td>{{name.firstName}}</td>
- <td>{{name.lastName}}</td>
- </tr>
- </table>
There are many built-in directives in AngularJS. Please find all the directives listed here: AngularJS Directives
Demo application
- <!DOCTYPE html>
- <html>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
-
- <body ng-app="" ng-init="Firstapp='Hello fisrt angularjs application'"> <span ng-bind="Firstapp"> </span></br> number expression example: 5+5 = {{ 5+5 }} </br>
- <p>First Name: <input type="text" ng-model="firstName"></p>
- <p>Last Name: <input type="text" ng-model="lastName"></p> string expression example with directives: <span>full Name: {{ firstName + " " + lastName }} </span></br> ng-repeat example:
- <table border="1" ng-init="fullNames=[{firstName:'Arvind',lastName:'Singh'},{firstName:'Risha',lastName:'Baghel'},
-
- {firstName:'Janvi',lastName:'Baghel'}]">
- <tr>
- <td>First Name</td>
- <td>Last Name</td>
- </tr>
- <tr ng-repeat="name in fullNames">
- <td>{{name.firstName}}</td>
- <td>{{name.lastName}}</td>
- </tr>
- </table>
- </body>
-
- </html>
In the above demo, I have used expressions, directives - ng-bind, ng-model, ng-repeat, and displayed the data in table.
Output