Every widget
, directive
and markup is defined with a compile function, which the angular compiler
executes on each widget or directive it encounters. The compile function optionally returns a link
function. This compilation process happens automatically when the page is loaded when you specify
ng:autobind
in the script tag from which you load the angular script file. (See Initializing Angular.)
The compile and link functions are related as follows:
repeater widget
used in a list element (<li ng:repeat="[item in
dataset]"
), the link function gets called to set up a listener on each element in the list.Note that angular's built-in widgets, directives, and markup have predefined compile and link
functions that you don't need to modify. When you create your own widgets, directives, or markup,
you must write compile and link functions for them. Refer to the Compiler API
for details.
When the angular compiler compiles a page, it proceeds through 3 phases: Compile, Create Root Scope, and Link:
Compile Phase
The result of the compilation phase is an aggregate link function, which comprises all of the individual link functions.
Create Root Scope Phase
Inject all services into the root scope.
Link Phase
Note that while the compile function is executed exactly once, the link function can be executed multiple times, for example, once for each iteration in a repeater.
The angular compiler exposes methods that you will need to make use of when writing your own
widgets and directives. For information on these methods, see the Compiler API doc
.