Wednesday, May 30, 2018

AngularJS Custom Directives

AngularJS custom directives are one of the most powerful components of AngularJS, helping you extend basic HTML elements/attributes and create reusable code.


Custom Directive types:

In AngularJS we can create the custom directive for the following types of elements. 

Element directives:

Directive activates when a matching element is encountered. Restrict mode is defined by "E".

Example: <ng-directives></ng-directives>

Attribute: 

Directive activates when a matching attribute is encountered. Restrict mode is defined by "A".

Example: <span ng-directive></span>

CSS: 

Directive activates when a matching css style is encountered. Restrict mode is defined by "C".

Example: <span class="ng-directive"></span>

Comment:

Directive activates when a matching comment is encountered. Restrict mode is defined by "M".

Example: <!-- directive: ng-directive -->

How to create custom directives:
Example 1:

  1. <html>  
  1.   
  1. <head>  
  1.     <title>Angular JS Custom Directive Example</title>  
  1.     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js">  
  1.         </script>  
  1.         <style>  
  1.             .sparkline  
  1.             {  
  1.                 background-color: brown;  
  1.                 font-size: large;  
  1.                 height: 100px;  
  1.                 width: 200px;  
  1.                 color: aqua  
  1.             }  
  1.         </style>  
  1.   
  1. </head>  
  1.   
  1. <body>  
  1.     <h2>AngularJS Custom Directive Example</h2>  
  1.     <div ng-app="mainApp">  
  1.         <divng-demo>  
  1.             </div>  
  1.             </div>  
  1.   
  1. </body>  
  1. <script>  
  1.     var mainApp = angular.module("mainApp", []);  
  1.     mainApp.directive('ngDemo', function()  
  1.     {  
  1.         return  
  1.         {  
  1.             restrict: 'A',  
  1.             template: '<div class="sparkline">Simple Custom Directive of Element Type</div>'  
  1.         }  
  1.     });  
  1. </script>  
  1.   
  1.   
  1. </html>  
In the above code we defined the name of the directive (ngDemo). Type of the directive is an attribute that is defined by restrict: 'A'. Here the restrict option is used to define the type of element for which this directive will invoke.

In template option we define the text (template) that replaces the content of the element that invokes this directive. Template is an option where we are specifying the html that will be appended or append.

<divng-demo></div>


Using the above code we invoked the directive. Notice that when we invoke it (ng-demo) and the name of directive (ngDemo), both are not the same. This is because AngularJS will handle translating the camel cased name when we define it to the snake case when we invoke it.


Click here to read about data binding.

2 comments:

  1. I like your post very much. It is very much useful for To me. I hope you to share more info about this. Keep posting...

    Angular JS Online training
    Angular JS training in hyderabad

    ReplyDelete
  2. Have any concern like Which is the Best AngularJS Development Company? And perplexed over choosing a development framework for your web application. Then we suggest you go with the AngularJS framework for web development and hire Angular JS developer for your web app development project. There are several prominent names among the Angularjs development companies, you can consider for your web application requirement.

    ReplyDelete

Whats new in Angular 8

Angular 8 was released in May 2019 and  th e key features of angular 8 are as follows in brief, Differential loading Dynamic imports ...