I have a few doubts about this functions.
Lets say I have this directive:
.directive('hello', function () {
return {
template: '<div>Hello <span ng-transclude></span></div>',
restrict: 'E',
transclude: true,
compile: function() {
console.log('Compile()');
return {
pre: function() {
console.log('PreLink()');
},
post: function() {
console.log('PostLink()');
}
};
},
link: function postLink(scope, element, attrs) {
console.log('Link()');
}
};
}
And I add it to my template as:
<hello>World</hello>
The console logs:
Compile()
PreLink()
PostLink()
So why is the link()
not being called?
If instead of returning an object from compile()
I return a single function printing PreLink()
the console logs:
Compile()
PreLink()
If I don't return anything from Compile()
the console logs:
Compile()
Still link()
is not being called.
If I just comment Compile()
then Link()
is finally printed:
Link()
Can someone explain all this? Are Link()
and Compile()
mean to work together? Should I just use Compile's PreLink()
and PostLink()
?