Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Sunday 7 February 2016

Directives in AngularJS

In this Article, we will learn about Directives in AngularJS Application. Before starting, I suggest you to read my previous articles in this series.


Directives are one of the core feature 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 Directive. 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 application. 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>
    <!--Here we initialize Name property using ng-init Directive-->
    <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>
    <!--Here we initialize Names property using ng-init Directive-->
    <div ng-init="Names=['Anoop','Akshay','Ajay','Abhishek','Arjun']">
        <h3>ng-repeat Example</h3>
        <ul>
            <!--ng-repeat directive repeats html elements for each item in a collection.-->
            <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>
    <!--Here we initialize Employees property using ng-init Directive-->
    <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>
            <!--ng-repeat directive repeats html elements for each item in a collection.-->
            <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 Element 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>
    <!--Binding checkbox value with isVisible property using ng-model directive-->
    <input type="checkbox" ng-model="isVisible" /> Show Div
    <!--Here we pass isVisible expression to ng-show Directive-->
    <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>
    <!--Binding checkbox value with isHidden property using ng-model directive-->
    <input type="checkbox" ng-model="isHidden" /> Hide Div
    <!--Here we pass isHidden expression to ng-hide Directive-->
    <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>
    <!--Here we initialize count property using ng-init Directive-->
    <div ng-init="count=0">
        <h3>ng-click Example</h3>
        <!--Increasing and Decreasing the value of count expression using ng-click directive-->
        <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 https://docs.angularjs.org/api/ng/directive for reading more about various built in directive.
Hope you liked it. Thanks!
[Download Source Code via Google Drive]

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook