AngularJS Compilation Process

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 traverses the DOM and collects 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, 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 the templates in other JavaScript framework/library like backbone and jQuery, they process the template as a string and result as a string. You have to dump this result string into the DOM, where you wanted it with .innerHTML()

AngularJS processes 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 the model and view to synchronize your data.

How Angular's directives are compiled

It's important to note that Angular operates on DOM nodes, instead of the strings. Usually, you don't notice this , because when 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 the directives.

  2. Once the entire DOM has been traversed, it will sort the list of directives by their priority. Each directive compiles its own functios, that 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 return.

  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

  1. var $compile = ...;  
  2.   
  3. // injected into your code  
  4.   
  5. var scope = ...; var parent = ...;  
  6.   
  7. // DOM element where the compiled template can be appended  
  8.   
  9. var html = '<div ng-bind="exp"></div>';  
  10.   
  11. // Step 1: parse HTML into DOM element var template = angular.element(html);  
  12.   
  13. // Step 2: compile the template var linkFn = $compile(template);  
  14.   
  15. // Step 3: link the compiled template with the scope. var element = linkFn(scope);  
  16.   
  17. // Step 4: Append to DOM (optional) parent.appendChild(element);

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now