What is the difference between compile and link function in angularjs ?

What is the difference between compile and link function in angularjs ?

In compile phase, the angular parser starts parsing the DOM and whenever the parser encounters a directive it creates a function. These functions are termed as a template or compiled functions. In this phase, we do not have access to the $scope data. In the link phase, the data i.e. ($scope) is attached to the template function and executed to get the final HTML output.

Compile – It works on a template. It’s like adding a class element into the DOM (i.e., manipulation of tElement = template element), hence manipulations that apply to all DOM clones of the template associated with the directive.

Link – It works on instances. Usually used for registering DOM listeners (i.e., $watch expressions on the instance scope) as well as instance DOM manipulation. (i.e., manipulation of iElement = individual instance element).

Leave a comment

Start a Blog at WordPress.com.

Up ↑