javascript - AngularJS : ng-repeat not executing inside scoped directive

标签 javascript angularjs angularjs-directive angularjs-scope angularjs-ng-repeat

遇到 ng-repeat 似乎没有在自定义指令内执行的问题。我认为问题与嵌入/范围有关,但我不太确定。

指令:

.directive('node', ['$compile', function($compile) {
    return {
        restrict: 'E',
        scope: {
            obj: '=obj',
        },
        transclude: true,
        link: function(scope, element, attrs, ctrl, transclude) {
            transclude(scope, function(clone, scope) {
                console.log(clone);
                var tmp = angular.element('<div></div>'), template;
                tmp.append(clone);
                if(scope.obj.hasSub) {
                    template =
                        '<div>' +
                          '<span>{{obj.name}}</span>' +
                          '<ul>' +
                              '<li ng-repeat="child in obj.children">' +
                                '<node data-obj="child" >' + tmp.html() + '</browser-node>' +
                              '</li>' +
                          '</ul>' +
                        '</div>';
                } else {
                  template = 
                    '<div>' +
                      tmp.html() +
                    '</div>'
                }
                element.html('').append($compile(template)(scope));
            });
        }
    };
}]);

Controller :

.controller('Main', ['$scope', function($scope) {
  $scope.items = [
    {'name': 'one', hasSub: true, children: [
      { 'name': 'one-one', items: { foo: 3, bar: 2 } } 
    ] },
    {'name': 'two', hasSub: true, children: [
      { 'name': 'two-one', items: { foo: 6, bar: 5 } } 
    ] }
  ];
}]);

html:

   <ul>
  <li ng-repeat="item in items">
    <node obj="item">
      {{obj.name}}
      <ul>
        <li ng-repeat="(k,v) in obj.items">{{k}}: {{v}}</li>
      </ul>
    </node>
</ul>

item节点的名称显示正常,{{obj.items}}会输出正确的内容,但ng-repeat似乎什么也没做。

Plunker

最佳答案

您的数据的第一级中没有属性items。如果您将 items 的转发器嵌套在模板中并更改为 child.items,则效果很好

template =
    '<div>' +
      '<span>{{obj.name}}</span>' +
      '<ul>' +
          '<li ng-repeat="child in obj.children">' +
            '<node data-obj="child" >' + tmp.html() + '</node>' +
            '<ul>'+
            '<li ng-repeat="(k,v) in child.items">{{k}}: {{v}}</li>'+
          '</ul>'+
          '</li>' +
      '</ul>' +
    '</div>';

DEMO

关于javascript - AngularJS : ng-repeat not executing inside scoped directive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25858910/

相关文章:

javascript - 无法在 Firefox 4/IE 中更改 iFrame 内容

javascript - 如何使用 WebDriverJS 捕获 Selenium 错误

javascript - 如何使用d3在散点图上一一绘制/添加节点?

javascript - AngularJS 选择框 - ngClick 未触发

javascript - HTML5 Canvas : How do I merge 2 canvas into 1 (of which 1 is draggable)

javascript - 扩展 ngClick 以添加附加功能

javascript - Angularjs:如何在输入时使输入[文本] ngModel延迟值

javascript - 如果我们有 5 秒后隐藏的时间间隔,则无法获取 Growl 消息

angularjs - 如何在 iframe 之外操作历史记录

javascript - 通过追加方法调用 Angular Directive(指令)