Please enable Javascript to correctly display the contents on Dot Net Tricks!

A Look into AngularJS Compilation Process

  Author : Shailendra Chauhan
Posted On : 15 Aug 2014
Total Views : 10,668   
Updated On : 15 Aug 2014
 

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

AngularJS compilation process takes place in the web browser; no server side or pre-compilation step is involved. Angular use $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 :

  1. Compile

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

  2. Link

    It combine the directives with a scope and produce 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, where you first compile the code and then link it to actually execute it. The process is very much similar in AngularJS as well.

Comparing Angular template compilation with other framework template compilation

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 manipulate it as required. It uses two way data-binding between model and view to sync your data.

How angular's directives are compiled

It's important to note that Angular operates on DOM nodes rather than strings. Usually, you don't notice this because when a 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 are 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 $watchs 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 do you think?

I hope you have better understanding of AngularJS compilation process. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS (0)
6 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST

Know More
30 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

Know More
28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

27 MAY
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:00 PM-6:30 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

22 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM Weekend

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM Weekend

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+