Javascript ng repeat的Wrapping指令未找到子元素
我希望我的wrapping指令获取其中的元素并将事件绑定到它们。 我的标记如下:Javascript ng repeat的Wrapping指令未找到子元素,javascript,angularjs,Javascript,Angularjs,我希望我的wrapping指令获取其中的元素并将事件绑定到它们。 我的标记如下: <div wrapping-directive> <div ng-repeat='item in items'></div> </div> app.directive('wrappingDirective', function() { return function(scope, element, attrs) { var items
<div wrapping-directive>
<div ng-repeat='item in items'></div>
</div>
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( //
}
}
});
app.directive('eventDirective',function(){
返回{
链接:函数(范围、元素、属性){
$(元素)。单击(//
}
}
});
或者将指令本身注入列表
<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){
//
}
}
}
});
app.directive('wrappingDirective',function(){
返回{
范围:{
“项目”:“=”
},
模板:“”,
链接:函数(范围、元素、属性){
$scope.doSomething=功能(项目){
//
}
}
}
});