【问题标题】:angularjs repeat custom directiveangularjs重复自定义指令
【发布时间】:2015-09-10 18:30:13
【问题描述】:

我想从 ng-repeat 中的自定义指令中输出自定义指令以用于拖放目的。我的问题是 myWidget 正确输出 html-tag 但自定义指令未执行。

类似这篇文章: ng-repeat in combination with custom directive

主指令模板:

<div class="col-lg-5 hidden-md main-column" style="height:inherit;"
         ng-model='list2'
         data-drop="true"
         data-jqyoui-options="{accept:'div:not([ng-model=list2])'}"
         jqyoui-droppable="{multiple:true}">

        <div ng-repeat="item in list2"
             ng-model="list2"
             data-drag="{{item.drag}}"
             data-jqyoui-options="{revert: 'invalid'}"
             jqyoui-draggable="{index: {{$index}},animate:true}">
                <div my-widget="item.widget"></div>
        </div>

动态调用指令:

(function(){
"use strict";

angular
    .module('achilles')
    .directive('myWidget', function () {
        return {
            scope: {
                w: '=myWidget'
            },
            restrict: 'EA',
            priority: 300,
            link: function(scope, element, attrs) {
                element.replaceWith('<' + scope.w + '>');
            }
        };
    });})();

inspector 显示输出是但指令没有被触发。

(function(){
"use strict";

angular
    .module('achilles')
    .directive('anamnesisWidget', anamnesisWidget);

是否存在某种优先级或范围问题导致 3. 指令不触发?

【问题讨论】:

  • 输出为:

标签: angularjs angularjs-directive ng-repeat


【解决方案1】:

在替换它之前,您需要使用$compile compile 该指令元素,以便调用底层指令并呈现它元素。

代码

link: function(scope, element, attrs) {
    element.replaceWith($compile('<' + scope.w + '/>')(scope));
}

【讨论】:

  • 我不得不注入 $compile 然后它工作。为什么我在 $compile 之后需要(范围)?
  • @user2436745 我没有在你的代码中提到 $compile 部分。它在 Angular 中的常见做法是在使用它们之前注入服务。$scope 告诉 Angular 进行转换(编译) element 在 Angular 上下文中将有这个 scope 可用..
猜你喜欢
  • 1970-01-01
  • 2014-06-24
  • 2015-06-21
  • 2023-03-27
  • 2017-05-26
  • 2014-03-07
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
相关资源
最近更新 更多