ng的包装指令repeat找不到子元素



我希望我的包装指令能够获取其中的元素并将事件绑定到它们。我的标记是这样的:

<div wrapping-directive>
   <div ng-repeat='item in items'></div>
</div>

我想要的是在包装指令中重复ng创建的所有项目,如下所示:

app.directive('wrappingDirective', function() {
    return function(scope, element, attrs) {
        var items = element.find('div'); // this returns an empty array
        // if i write $(element).find('div'); it returns an empty array as well
    }
});

也许可以将指令直接放在项上,并为它们提供事件。

<div>
   <div ng-repeat='item in items' event-directive></div>
</div>
app.directive('eventDirective', function() {
    return {
      link: function(scope, element, attrs) {
          $(element).click( //
      }        
    }
});

或者将指令本身插入到列表中。

<div wrapping-directive>
</div>
app.directive('wrappingDirective', function() {
    return {
       scope: {
         "items": "="
       },
       template:"<div ng-repeat='item in items' ng-click='doSomething(item)'></div>",
       link: function(scope, element, attrs) {
           $scope.doSomething = function(item){
            //
           }
       }
    }
});

最新更新