Introduction
Directives are one of the most important component of AngularJS application. They are extended HTML attributes. In other words, directives are something that introduces new syntax. They are markers on the DOM element which provides some special behavior to DOM elements and tell AngularJS's HTML compiler to attach.
Their are many built-in directives such as ng-model, ng-repeat, ng-show, ng-bind etc. All these directives provide special behavior to DOM elements. For example, ng-show directive conditionally shows an element, ng-click directive adds click events to the element; ng-app directive initializes an AngularJS application, etc.
Custom directives
Custom directives are used to extend the functionality of HTML. They are defined by using "app.directive()" function in AngularJS. Custom directives replace the element for which they are active. Using "compile" method, AngularJS application finds the matching element and replace with directive definition. This is one time activity HTML compile and load.
Types of Directives
Type of directive determines how they are used. We can implement directives by the following ways:
- Attribute directives: Directive is active when matching attribute is found.
Example:
- <input type="text" numeric />
- Element directives: Directive is active when matching element is found.
Example:
- <numeric-Textbox id = "txtAge" />
- Component directives: Directive is active when matching component is found.
Example:
- <!-- directive: numeric-Textbox exp -->
- CSS class directives: Directive is active when matching CSS style is found.
Example:
- <input type="text" class=" numeric "/>
Directives are registered same way as controller and directive returns a simple object that has many properties to configure the directives. "AngularJS application name.directive" function is used to register a new directive in our module. The first argument to this function is the name of directive and second argument to this function is function that returns a directive definition object. If the directive has any external dependencies such as services, $scope object, etc can be injected in this function.
Hello World Example
The example directive shows static text. The following code shows Hello World directive.
- app.directive('helloWorld', function () {
- return {
- template: '<h3>Hello World!!</h3>'
- };
- });
This directive can be used in HTML as in the following way:
As element:
As attribute
If we want to be HTML5 compliant, we can use x- or data- as prefix. The following markup will match hello world directive.
- <div data-hello-world></div>
The following are the important properties of directive:
Child Scope in directive example
The following is the child scope example with directive. Here scope property of the directive is set to true, so directive will use the parent scope.
Directive code:
- app.directive('helloUser', function () {
- return {
- scope: true,
- template: '<h3>Hello {{Myname}}!! </h3>'
- };
- });
HTML implementation
- <div ng-controller="demoController">
- <b>Directive with Parent's scope example</b>
- <br />
- <br />
- Enter Your Name: <input type="text" ng-model="Myname" />
- <hello-user />
- </div>
Controller Code:
- app.controller("demoController", function ($scope) {
- $scope.Myname = "Jignesh";
- });
Output:
Isolated Scope in directive example
The following is isolated scope example with directive. Here scope property of the directive contains the scope variable name and value with prefix. From the prefix AngularJS decides, from where value can be found. Normally the following three prefixes are used in directive:
- "@" uses to read the attribute value.
- "=" provides two-way binding.
- "&" works with functions.
Directive code:- app.directive('helloIsolatedScope', function () {
- return {
- scope: {
- testName: "=name"
- },
- template: '<h3>Hello {{testName}}!! </h3>'
- };
- });
HTML implementation- <div ng-controller="demoController">
- <b>Directive with Isolated Scope example</b>
- <hello-isolated-scope name="Test" />
- </div>
Controller Code:- app.controller("demoController", function ($scope) {
- $scope.Myname = "Jignesh";
- $scope.Test = "Jignesh Trivedi";
- });
Code relation diagram:
Summary
This post is helping us to learn how to create directives in AngularJS application, type of directive, scope of directive, etc. Directives are really great feature of AngularJS and it is able to extend our HTML and we can reuse our HTML with the help of directive.