Compiler is an Angular service which traverses the DOM looking for attributes. The compilation process happens in two phases.
- Compile: traverse the DOM and collect all of the directives. The result is a linking function.
- Link: combine the directives with a scope and produce a live view. Any changes in the scope model are reflected in the view, and any user interactions with the view are reflected in the scope model. This makes the scope model the single source of truth.
A directive is just a function which executes when the compiler encounters it in the DOM.
Directives often have a link function. But it’s rare for directives to have a compile function, since most directives are concerned with working with a specific DOM element instance rather than changing its overall structure.
When declaring a directive, use
ngTransclude in template together with the
tranclude: true option to enable transclusion. The contents of a transcluded directive have whatever scope is outside the directive. So it’s not affected by the isolated scope of the directive itself.
The second demo of Creating a Directive that Wraps Other Elements in angular document is a good one to demonstrate this.
And it states very clear why isolated scope and transclude scope of a directive are siblings:
This behavior makes sense for a directive that wraps some content, because otherwise you’d have to pass in each model you wanted to use separately. If you have to pass in each model that you want to use, then you can’t really have arbitrary contents, can you?
There are two demos that demonstrate how transclusion and compile function may be used in a directive in this blog post.
The goal behind the expressions that you use in templates is to let you be as clever as you need to be to create hooks between your template, your application logic, and your data, but at the same time prevent application logic from sneaking into the template.
Controllers have three responsibilities in your app:
Uncaught NotFoundError: An attempt was made to reference a Node in a context where it does not exist.