0

I am very new to angularjs. I have little bit confusion with link and compile usage in directives. Can anyone please tell me in which scenarios we have to use link and compile.

priya
  • 31
  • 2
  • 1
    Possible duplicate of [What is the difference between compile and link function in angularjs](http://stackoverflow.com/questions/12164138/what-is-the-difference-between-compile-and-link-function-in-angularjs) – kukkuz Jul 25 '16 at 06:37
  • Do some research please before asking questions here. – M. Junaid Salaat Jul 25 '16 at 06:59

2 Answers2

0

What is the difference between compile and link function in angularjs

Already answered on stack overflow and has an excellent explanation

Community
  • 1
  • 1
Srijith
  • 1,434
  • 9
  • 14
0

The compile phase

When the DOM is loaded Angular starts the compile phase, where it traverses the markup top-down, and calls compile on all directives. Graphically, we could express it like so:

An image illustrating the compilation loop for children

It is perhaps important to mention that at this stage, the templates the compile function gets are the source templates (not instance template).

The link phase

DOM instances are often simply the result of a source template being rendered to the DOM, but they may be created by ng-repeat, or introduced on the fly.

Whenever a new instance of an element with a directive is rendered to the DOM, the link phase starts.

In this phase, Angular calls controller, pre-link, iterates children, and call post-link on all directives, like so:

Below links will give you clear idea for compile vs link.

Angular directives - when and how to use compile, controller, pre-link and post-link

http://odetocode.com/blogs/scott/archive/2014/05/28/compile-pre-and-post-linking-in-angularjs.aspx

Community
  • 1
  • 1
Jaldhi Oza
  • 92
  • 5