Check out http://jsfiddle.net/3jvoL4ew/
(Note: I know it can be done with a template:
, that's not my question though)
It contains a directive that adds a button and a text to the element it is used on:
app.directive("otcDynamic", function ($compile) {
return {
compile: function(element) {
var template = "<button ng-click='doSomething()'>{{label}}</button>";
element.append(template);
}
}
});
It works, but the usual way such a directive is defined is:
app.directive("otcDynamic", function ($compile) {
return {
link: function (scope, element) {
var template = "<button ng-click='doSomething()'>{{label}}</button>";
var linkFn = $compile(angular.element(template));
var content = linkFn(scope);
element.append(content);
}
}
});
So if using compile:
is more concise, and more efficient when used in ngRepeat, why do people recommend using $compile
?