Powered By Blogger

Wednesday, July 15, 2015

Angular Compilation

Angular's HTML compiler allows you to teach the browser new HTML syntax. The compiler allows you to attach new behaviors or attributes to any HTML element. Angular calls these behaviors as directives.

AngularJS compilation process takes place in the web browser; no server side or pre-compilation step is involved. Angular uses $compiler service to compile your angular HTML page. The angular' compilation process begins after your HTML page (static DOM) is fully loaded. It happens in two phases:

Compile - It traverse the DOM and collect all of the directives. The result is a linking function.

Link - It combines the directives with a scope and produces a live view. Any changes in the scope model are reflected in the view, and any user interactions with the view are reflected in the scope model.

The concept of compile and link comes from C language, where you first compile the code and then link it to actually execute it. The process is very much similar in AngularJS as well.

If you have worked on templates in other java script framework/library like backbone and jQuery, they process the template as a string and result as a string. You have to dumped this result string into the DOM where you wanted it with innerHTML()

AngularJS process the template in another way. It directly works on HTML DOM rather than strings and manipulates it as required. It uses two way data-binding between model and view to sync your data.

It is important to note that Angular operates on DOM nodes rather than strings. Usually, you don't notice this because when an html page loads, the web browser parses HTML into the DOM automatically.

HTML compilation happens in three phases:

1. The $compile traverses the DOM and looks for directives. For each directive it finds, it adds it to a list of directives.

2. Once the entire DOM has been traversed, it will sort that list of directives by their priority. Then, each directive’s own compile function is executed, giving each directive the chance to modify the DOM itself. Each compile function returns a linking function, which is then composed into a combined linking function and returned.

3. $compile links the template with the scope by calling the combined linking function from the previous step. This in turn will call the linking function of the individual directives, registering listeners on the elements and setting up $watch with the scope as each directive is configured to do.

The pseudo code for the above process is given below:

 var $compile = ...;   
 // injected into your code   
 var scope = ...;   
 var parent = ...;   
 // DOM element where the compiled template can be appended   
 var html = '<div ng-bind="exp"></div>';   
 // Step 1: parse HTML into DOM element   
 var template = angular.element(html);   
 // Step 2: compile the template   
 var linkFn = $compile(template);   
 // Step 3: link the compiled template with the scope.   
 var element = linkFn(scope);   
 // Step 4: Append to DOM (optional)   
 parent.appendChild(element);  

What are Compile, Pre, and Post linking in AngularJS?

Compile – This compiles an HTML string or DOM into a template and produces a template function, which can then be used to link scope and the template together.

Use the compile function to change the original DOM (template element) before AngularJS creates an instance of it and before a scope is created.


Post-Link – This is executed after the child elements are linked. It is safe to do DOM transformation in the post-linking function.
Use the post-link function to execute logic, knowing that all child elements have been compiled and all pre-link and post-link functions of child elements have been executed.

Pre-Link – This is executed before the child elements are linked. Not safe to do DOM transformation since the compiler linking function will fail to locate the correct elements for linking.
Use the pre-link function to implement logic that runs when AngularJS has already compiled the child elements, but before any of the child element's post-link functions have been called.

 <html>   
 <head>   
 <title>Compile vs Link</title>   
 <script src="lib/angular.js"></script>   
 <script type="text/javascript">   
 var app = angular.module('app', []);   
 function createDirective(name) {   
   return function () {   
    return {   
       restrict: 'E',   
       compile: function (tElem, tAttrs) {   
          console.log(name + ': compile');   
          return {   
            pre: function (scope, iElem, attrs) {   
              console.log(name + ': pre link');   
            },  
            post: function (scope, iElem, attrs) {   
              console.log(name + ': post link');   
            }  
          }  
        }   
     }   
    }   
 }   
 app.directive('levelOne', createDirective('level-One'));   
 app.directive('levelTwo', createDirective('level-Two'));   
 app.directive('levelThree', createDirective('level-Three'));   
 </script>   
 </head>   
 <body ng-app="app">   
 <level-one>   
   <level-two>   
     <level-three> Hello {{name}} </level-three>   
  </level-two>   
 </level-one>   
 </body>   
 </html>  

Output


No comments:

Post a Comment