I'm trying to write an Angular 1 directive that will dynamically add other directives to the element. These other directives are complicated enough to where I don't want to copy and paste their code into this meta directive. I have created a plnkr showing what I'm trying to accomplish here.
Here's a copy of the directive
function myCustomAttribute($compile) {
var priority = 1001; // greater than ng-repeat
return {
restrict: 'A',
compile: compile,
priority: priority,
terminal: true,
};
function compile(templateElement, templateAttributes, transcludeFn) {
var attrValue = templateAttributes[attributeDirectiveName];
templateAttributes.$set(colorDirectiveName, attrValue);
var compiled = $compile(templateElement, transcludeFn, priority);
return function linkFn(scope) {
compiled(scope);
};
}
function doubleCompile(templateElement, templateAttributes, transcludeFn) {
var attrValue = templateAttributes[attributeDirectiveName];
templateAttributes.$set(colorDirectiveName, attrValue);
templateElement.removeAttr('my-custom-attribute');
return function linkFn(scope, element, attrs, ctrls, transcludeFn) {
$compile(element, transcludeFn)(scope);
};
}
}
The compile
function comes from the angular documentation on the double compilation issue. As the plnkr shows, it has issues when used with a directive that has transclude: true
and an isolate scope. It also has issues inside of a nested ng-repeat.
The doubleCompile
function comes from Add directives from directive in AngularJS. As the plnkr shows, it has issues with double compiling directives with a higher priority. It also has the same transclude issue.
The plnkr is a minified example of what I'm trying to solve in my application, all code except the myCustomAttribute directive is for highlighting issues. How do I update myCustomAttribute
so that it works in all cases?