Link vs compile vs controller
Asked 07 September, 2021
Viewed 3K times
  • 67
Votes

When you create a directive, you can put code into the compiler, the link function or the controller.

In the docs, they explain that:

  • compile and link function are used in different phases of the angular cycle
  • controllers are shared between directives

However, for me it is not clear, which kind of code should go where.

E.g.: Can I create functions in compile and have them attached to the scope in link or only attach functions to the scope in the controller?

How are controllers shared between directives, if each directive can have its own controller? Are the controllers really shared or is it just the scope properties?

6 Answer