According to the documentation a template
can be a function which takes two parameters, an element
and attributes
and returns a string value representing the template. It replaces the current element with the contents of the HTML. The replacement process migrates all of the attributes and classes from the old element to the new one.
The compile
function deals with transforming the template DOM. It takes three parameters, an element
, attributes
and transclude
function. The transclude
parameter has been deprecated. It returns a link
function.
It appears that a template
and a compile
functions are very similar and can achieve the same thing. The template
function defines a template and compile
function modifies the template DOM. However, it can be done in the template
function itself. I can't see why modify the template DOM outside the template
function. And vice-versa if the DOM can be modified in the compile
function then what's the need for a template
function?
One of the best uses of the template function is to conditionally generate a template. This allows you to automate the creation of a template based on an attribute.
I have seen some very large templates that use
ng-if
to hide sections of the template. But instead it tossing everything into the template and usingng-if
, which can cause excessive binding you can remove sections of the DOM from the template that will never be used.Let's say you have a directive that will include either sub-directive
item-first
oritem-second
. And the sub-directive will not ever change for the live of the outer directive. You can adjust the template, prior to the compile function being called.And the template string for these would be:
and
I agree that this is an extreme simplification, But I have some very complicated directives and the outer directive needs to display one of, about, 20 different inner directives based on a type. Instead of using transclude, I can set the type on the outer directive and have the template function generate the correct template with the correct inner directive.
That correctly formatted template string is then passed on to the compile function, etc.
The compilation function can be used to change the DOM before the resulting template function is bound to the scope.
Consider the following example:
You can use the compile function to change the template DOM like this:
So you can use the
compile
function to change the template DOM to whatever you like if your directive requires it.In most cases
tElem
andiElem
will be the same DOM element, but sometimes it can be different if a directive clones the template to stamp out multiple copies (cf.ngRepeat
).Behind the scenes, Angular uses a 2-way rendering process (compile + link) to stamp out copies of a compiled piece of DOM, to prevent Angular from having to process (= parse directives) the same DOM over and over again for each instance in case the directive stamps out multiple clones resulting in much better performance.
Hope that helps!
ADDED AFTER COMMENT:
The difference between a
template
andcompile
function:Template function
Compile function
The template function is called before the compile function is called.
Although they can perform almost identical stuff and share the same 'signature', the key difference is that the return value of the template function will replace the content of the directive (or the complete directive markup if
replace: true
), while a compile function is expected to change the DOM programmatically and return a link function (or object with pre and post link function).In that sense you can think of the template function as some kind of convenience function for not having to use the compile function if you simply need to replace the content with a string value.
Hope that helps!