I am here to continue the discussion around AngularJS. Today, we will go through one of the main features of the AngularJS i.e., custom directive. Also in case you have not had a look at our previous articles of this series, go through the following links:
- Learn AngularJS From Beginning: Basic - Part 1
- Learn AngularJS From Beginning: Filter - Part 2
- Learn AngularJS From Beginning: Service - Part 3
In the previous articles, we discussed the AngularJS basic structure including control and model. Also, we discussed how to use the built-in filter how to create custom filters, and about AngularJS service. Now, in this article, we will discuss the directive concept in Angular JS.
Actually from a user’s point of view, directives are nothing more than custom HTML tags that are added to application templates. Directives can be simple or they can be complex. Directives are used by the AngularJS HTML compiler to enhance the functionality of the associated template. Some examples of AngularJS directives are ngModel, ngView, and ngRepeat etc. The word Compiler related to AngularJS is often confusing to developers new to the framework. Actually, the word compiler takes on a whole new meaning in the context of AngularJS. Compiling HTML in AngularJS is simply the process of searching through the DOM tree to identify HTML elements associated with directives. The compiler then populates the template and assigns events to the associated elements in the templates.
What are Directives?
Now, it is a very common question in AngularJS -- what are directives? Directives are very valuable in AngularJS and what sets AngularJS apart from most javascript client-side frameworks. Due to the AngularJS inbuilt directives, we can avoid creating model classes with hundreds of lines of code and also, we have a simplified model and view in AngularJS that allows developers to quickly create powerful javascript applications. Directives are created using the Module. directive method and the arguments are the name of the new directive and a factory function that creates the directives.
Naming Convention for Custom Directives
Since HTML is a case-insensitive language, I always refer to a directive's name in a camel case like link-Menu in the HTML template file. The AngularJS HTML compiler then normalizes the directive name into its camel case equivalent, i.e. linkMenu. Also, remember that all directive names used in templates must be unique. Directive names cannot match with any existing HTML tag name or with the AngularJS inbuilt directives name.
The following attributes can be used during the creation of new AngularJS Directives.
- Restrict: The restrict attribute is how AngularJS triggers the directive inside a template. The default value of the restrict option is “A”. The value of “A” causes the directives to be triggered on the attribute name. Other than “A”, the restrict option has “E” (only matches the element name), “C” (only matches class name), and “M” (only matches the comment name) or any combination among the four options.
- TemplateUrl: The templateUrl attribute tells the AngularJS HTML compiler to replace the custom directive inside a template with HTML content located inside a separate file. The link-Menu (say, our custom directive name) attribute will be replaced with the content of our original menu template file.
- Template: Specify an inline template as a string. Not used if you’re specifying your template as a URL.
- Replace: if true, replace the current element. If false or unspecified, append this directive to the current element.
- Transclude: This lets you move the original children of a directive to a location inside the new template.
- Scope: Create a new scope for this directive rather than inheriting the parent scope.
- Controller: Create a controller that publishes an API for communicating across directives.
- Require: Require that another directive be present for this directive to function correctly.
- Link: Programmatically modify resulting DOM element instances, add event listeners, and set up data binding.
- Compile: Programmatically modify the DOM template for features across copies of a directive, as when used in other directives. Your compile function can also return link functions to modify the resulting element instances.
Compile and Link Functions
While inserting templates is useful, the really interesting work of any directive happens in its compile or its link function. The compile and link functions are named after the two phases Angular uses to create the live view for your application. Let’s take a high-level view of Angular’s initialization process, in order:
Script loads: Angular loads and looks for the ng-app directive to find the application boundaries.
Compile phase
In this phase, Angular walks the DOM to identify all the registered directives in the template. For each directive, it then transforms the DOM based on the directive’s rules (template, replace, transclude, and so on), and calls the compile function if it exists. The result is a compiled template function, which will invoke the link functions collected from all of the directives.
Link phase
To make the view dynamic, Angular then runs a link function for each directive. The link functions typically create listeners on the DOM or the model. These listeners keep the view and the model in sync at all times. So we’ve got the compile phase, which deals with transforming the template, and the link phase, which deals with modifying the data in the view. Along these lines, the primary difference between the compile and link functions in directives is that compile functions deal with transforming the template itself, and link functions deal with making a dynamic connection between model and view. It is in this second phase that scopes are attached to the compiled link functions, and the directive becomes live through data binding. These two phases are separate for performance reasons. Compile functions execute only once in the compile phase, whereas link functions are executed many times, once for each instance of the directive. For example, let’s say you use ng-repeat over your directive. You don’t want to call compile, which causes a DOM walk on each ng-repeat iteration. Instead, you want to compile once, then link.
Let’s take a look at the syntax for each of these again to compare. For compile, we have.
compile: function(compileElement, compileAttrs, transclude) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) {
// pre-link logic
},
post: function postLink(scope, iElement, iAttrs, controller) {
// post-link logic
}
};
}
And for the link, it is.
link: function postLink(scope, iElement, iAttrs) {
// Code goes here
}



Rahul Pushpendu BhaskarPosted Jun 8, 2016, 2:38 PM
Nice One ...thank you
Pradeep SahooPosted May 9, 2016, 10:18 PM
Nice . Thanks for sharing
Kuppurasu NagarajPosted May 9, 2016, 12:15 PM
Nice Sharing..
Pankaj Kumar ChoudharyPosted May 7, 2016, 10:50 AM
Nice explain Sir......
Vignesh ManiPosted May 7, 2016, 8:45 AM
nice one