Custom Directive In AngularJS

Introduction

AngularJS facilitates extending the HTML, with new attributes, called "Directives". It provides a set of built-in directives that offer functionality to your applications, like ng-app, ng-init, ng-model. AngularJS also enables us to define our own custom directives.

Using Code

Let's create a custom directive, with the following code.
  1. <script>  
  2.    var app = angular.module("myApp", []);  
  3.    app.directive("myOwnDirective"function() {  
  4.       return {  
  5.         template : "<h1>This is my directive!</h1>"  
  6.       };  
  7.    });  
  8. </script> 
We can call/use a custom directive in the following four ways.
  1. Attribute
    <div my-own-directive></div>

  2. Class
    <div class="my-own-directive"></div>

  3. Element name
    <my-own-directive></my-own-directive>

  4. Comment
  1. <script>  
  2. var app = angular.module("myApp", []);  
  3. app.directive("myOwnDirective"function() {  
  4.     return {  
  5.         restrict : "M",  
  6.         replace : true,  
  7.         template : "<h1>This is my directive!!</h1>"  
  8.     };  
  9. });  
  10. </script>  
  11.   
  12. <!-- directive: my-own-directive --> 
From the above discussion, we can see that we can use directives in different ways. It is always recommended to use the directives through tag name and attributes over comment and class names. It will be easier to determine what directives an element matches.
 
Note: You might be confused that directive name (myOwnDirective) is different when we use it (my-own-directive). Directive name follows camel case and the implementation follows hypen(-) in the next capital letter.
 
Properties of Custom Directive 
 
replace

The replace property in directives indicates that the element to which the directive is being applied (<my-own-directive> in that case) should remain (replace: false) and the directive's template should be appended as its child.
  1. <div ng-controller="Ctrl" class="ng-scope">  
  2.     <div class="ng-binding">hello</div>  
  3. </div>
With replace as false, it looks like below.
  1. <div ng-controller="Ctrl" class="ng-scope">  
  2.     <my-dir>  
  3.         <div class="ng-binding">hello</div>  
  4.     </my-dir>  
  5. </div> 
The below code sample tells how to use replace properties.
  1. var app = angular.module("myApp", []);    
  2. app.directive("myOwnDirective"function() {    
  3.     return {    
  4.         restrict : "A",    
  5.         replacetrue,  
  6.         template : "<h1>This is my directive!!</h1>"    
  7.     };    
  8. }); 
restrict

We can restrict calling custom directives by using restrict attribute. It means if we use restrict attribute, then directive works only for the same module. The attribute values are:

A -> Attribute
C -> Class
E -> Element name
M -> Comment

Default value is EA (Element and Attribute).
  1. var app = angular.module("myApp", []);  
  2. app.directive("myOwnDirective"function() {  
  3.     return {  
  4.         restrict "A",  
  5.         template : "<h1>This is my directive!!</h1>"  
  6.     };  
  7. }); 
The above directive will be available in attribute only, like:
 
<div my-own-directive></div>
 
transclude
 
It specifies whether to transfer and include the original inner content of the directive’s HTML markup in the destination markup (which is defined in the template) or not. It appends the content but replace property replaces the whole content.
  1. var app = angular.module("myApp", []);  
  2. app.directive("myOwnDirective"function() {  
  3.     return {  
  4.         restrict : "A",  
  5.         transclude "true",
  6.         template : "<h1>This is my directive!!</h1>"  
  7.     };  
  8. }); 
template

It specifies the HTML content that will be added to the HTML result of the directive use.
  1. var app = angular.module("myApp", []);    
  2. app.directive("myOwnDirective"function() {    
  3.     return {  
  4.         template "<h1>This is my directive!!</h1>"    
  5.     };    
  6. });   
Link
 
It facilitates adding more dynamic HTML content, based on the data.
  1. var app = angular.module("myApp", []);      
  2. app.directive("myOwnDirective"function() {      
  3.     return {    
  4.         restrict: 'E',  
  5.         transclude: 'true',  
  6.         template: '<span ng-transclude></span>',  
  7.         linkfunction(scope, element, attr){  
  8.             element.append("<strong>" + attr.name + "</strong>");  
  9.             if(attr.pid === '0'){  
  10.                 element.append("<br> No Project");  
  11.             }  
  12.             else {  
  13.                 element.append("<br> Project Exists");  
  14.             }  
  15.         }         
  16.     };      
  17. });   
  18.   
  19. <!-- Add below HTML content-->  
  20.   
  21. <div ng-app="myApp" ng-controller="myController">  
  22.     <div ng-repeat="user in user">  
  23.         <my-own-directive title="{{user}}">  
  24.             User Name :    
  25.         </my-own-directive>  
  26.     </div>  
  27. </div> 
Conclusion

We discussed how to use custom directives in AngularJS. Directives are useful in creating repeatable and independent code. It helps to make JavaScript code less messy and more organized.

Hope this helps.