我正在尝试建立一种机制,使我能够解析链接函数中的指令。
示例:
angular.module('directives', [])
.directive('myContainer', ['$compile', function ($compile) {
return {
restrict: "E",
replace: true,
link: function (scope, element, attrs) {
angular.forEach(scope.component.components, function(component){
var newScope = scope.$new()
newScope.component = component;
var elem = angular.element('<'+component.type+'>'+'</'+component.type+'>')
//Trying to compile directive to be resolved
var resolvedDirective = $compile(elem)(scope)
element.append(resolvedDirective)
})
}
});
问题是,“resolvedDirective”(由组件 => 类型定义)只是创建一个包含其他指令名称的标记,该名称稍后将被解析。
为了示例,我的机制被简化了(递归...)
希望我的问题足够清楚......
提前致谢!
最佳答案
您的问题需要更清楚。请更新您的指令所需的结果。
您可以遵循此模式。这个答案解释了很多关于如何在指令中添加编译指令
Angularjs directive add directives as attribute and bind them dynamically
在您的情况下,您想要附加新指令,编译并绑定(bind)它。
因此,尝试在编译阶段添加附加元素,并在 postLink 阶段使用$compile。
关于angularjs - 解析链接函数内的指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29197229/