A Look into AngularJS Compilation Process

12 Dec 2023
2 min read  

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. This functionality is a fundamental aspect covered in Angular Certification.

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.

What do you think?

Understanding AngularJS's compilation process is crucial, especially when diving into more complex applications. Your blog provides a great insight into this process, a valuable component covered extensively in Angular Certification courses. Feedback from readers will undoubtedly enrich the learning experience.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Learn to Crack Your Technical Interview

Accept cookies & close this