Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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 functions 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?

1 Answer

0 votes
by (106k points)

The difference between compile, controller and link is as follows:-

  • Compile: This is used when we need to modify the directive template, like adding new expression, append another directive inside this directive

  • controller: Controller is used when we need to share/reuse $scope data.

  • link: Link is a function which is used when we need to attach event handler or to manipulate DOM.

Browse Categories

...