angularjs - 解析链接函数内的指令

标签 angularjs angularjs-directive

我正在尝试建立一种机制,使我能够解析链接函数中的指令。

示例:

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/

相关文章:

angularjs - Angular js中的内容可编辑

javascript - AngularJS 在同一元素上有多个指令,$compile :multidir ERROR

javascript - 带括号的输入的 AngularJS 验证错误

angularjs - 在父指令和所有子指令之间进行通信的服务,angularjs

angularjs - angularjs指令中原始变量的两种方式绑定(bind)

angularjs - 使用 Controller 作用域数据调用指令

javascript - 如何从 Angular 日期选择器中禁用分钟和文本(东部标准时间)?

如果值在数组中,AngularJS 输入有效

angularjs - .net core、 Angular 和应用程序设置

javascript - 查看数字是否在 Firebase 中