When writing an Angular directive, one can use any of the following functions to manipulate the DOM behaviour, contents and look of the element on which the directive is declared:
- compile
- controller
- pre-link
- post-link
There seem to be some confusion as for which function should one use. This question covers:
Directive basics
- How to declare the various functions?
- What is the difference between a source template and an instance template?
- In which order the directive functions are executed?
- What else happens between these function calls?
Function nature, do's and dont's
Related questions:
- Directive: link vs compile vs controller.
- Difference between the 'controller', 'link' and 'compile' functions when defining an angular.js directive.
- What is the difference between compile and link function in angularjs.
- Difference between the pre-compile and post-compile element in AngularJS directives?.
- Angular JS Directive - Template, compile or link?.
- post link vs pre link in Angular js directives.
How to declare the various functions?
Compile, Controller, Pre-link & Post-link
If one is to use all four function, the directive will follow this form:
Notice that compile returns an object containing both the pre-link and post-link functions; in Angular lingo we say the compile function returns a template function.
Compile, Controller & Post-link
If
pre-link
isn't necessary, the compile function can simply return the post-link function instead of a definition object, like so:Sometimes, one wishes to add a
compile
method, after the (post)link
method was defined. For this, one can use:Controller & Post-link
If no compile function is needed, one can skip its declaration altogether and provide the post-link function under the
link
property of the directive's configuration object:No controller
In any of the examples above, one can simply remove the
controller
function if not needed. So for instance, if onlypost-link
function is needed, one can use:Compile function
Each directive's
compile
function is only called once, when Angular bootstraps.Officially, this is the place to perform (source) template manipulations that do not involve scope or data binding.
Primarily, this is done for optimisation purposes; consider the following markup:
The
<my-raw>
directive will render a particular set of DOM markup. So we can either:ng-repeat
to duplicate the source template (<my-raw>
), and then modify the markup of each instance template (outside thecompile
function).compile
function), and then allowng-repeat
to duplicate it.If there are 1000 items in the
raws
collection, the latter option may be faster than the former one.Do:
Do not
What is the difference between a source template and an instance template?
The fact that Angular allows DOM manipulation means that the input markup into the compilation process sometimes differ from the output. Particularly, some input markup may be cloned a few times (like with
ng-repeat
) before being rendered to the DOM.Angular terminology is a bit inconsistent, but it still distinguishes between two types of markups:
The following markup demonstrates this:
The source html defines
which serves as the source template.
But as it is wrapped within an
ng-repeat
directive, this source template will be cloned (3 times in our case). These clones are instance template, each will appear in the DOM and be bound to the relevant scope.Post-link function
When the
post-link
function is called, all previous steps have taken place - binding, transclusion, etc.This is typically a place to further manipulate the rendered DOM.
Do:
What else happens between these function calls?
The various directive functions are executed from within two other angular functions called
$compile
(where the directive'scompile
is executed) and an internal function callednodeLinkFn
(where the directive'scontroller
,preLink
andpostLink
are executed). Various things happen within the angular function before and after the directive functions are called. Perhaps most notably is the child recursion. The following simplified illustration shows key steps within the compile and link phases:To demonstrate the these steps, let's use the following HTML markup:
With the following directive:
Compile
The
compile
API looks like so:Often the parameters are prefixed with
t
to signify the elements and attributes provided are those of the source template, rather than that of the instance.Prior to the call to
compile
transcluded content (if any) is removed, and the template is applied to the markup. Thus, the element provided to thecompile
function will look like so:Notice that the transcluded content is not re-inserted at this point.
Following the call to the directive's
.compile
, Angular will traverse all child elements, including those that may have just been introduced by the directive (the template elements, for instance).Instance creation
In our case, three instances of the source template above will be created (by
ng-repeat
). Thus, the following sequence will execute three times, once per instance.Controller
The
controller
API involves:Entering the link phase, the link function returned via
$compile
is now provided with a scope.First, the link function create a child scope (
scope: true
) or an isolated scope (scope: {...}
) if requested.The controller is then executed, provided with the scope of the instance element.
Pre-link
The
pre-link
API looks like so:Virtually nothing happens between the call to the directive's
.controller
and the.preLink
function. Angular still provide recommendation as to how each should be used.Following the
.preLink
call, the link function will traverse each child element - calling the correct link function and attaching to it the current scope (which serves as the parent scope for child elements).Post-link
The
post-link
API is similar to that of thepre-link
function:Perhaps worth noticing that once a directive's
.postLink
function is called, the link process of all its children elements has completed, including all the children's.postLink
functions.This means that by the time
.postLink
is called, the children are 'live' are ready. This includes:The template at this stage will thus look like so:
Pre-link function
Each directive's
pre-link
function is called whenever a new related element is instantiated.As seen previously in the compilation order section,
pre-link
functions are called parent-then-child, whereaspost-link
functions are calledchild-then-parent
.The
pre-link
function is rarely used, but can be useful in special scenarios; for example, when a child controller registers itself with the parent controller, but the registration has to be in aparent-then-child
fashion (ngModelController
does things this way).Do not: