0 votes
1 view
in Web Technology by (47.8k 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 (107k 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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...