我尝试使用 ng-click
触发 test()
但它不起作用。不过, test()
函数在其他地方与 ng-click
一起使用,所以我假设它与它是一个 ng-repeat
ed 指令。
如何解决这个问题?
最佳答案
您的指令使用隔离
作用域,因此它无权访问其父作用域。
您需要使用 &
将方法从其隔离范围传递给指令
<div ng-repeat="day in thisMonth track by $index">
<drop-target index="$index" day="day" method="test()"></drop-target>
</div>
指令
angular.module('app.directives.dropTarget', [])
.directive('dropTarget', function() {
return {
restrict: 'E',
scope: {
day: '=',
index: '=',
method: '&'
},
templateUrl: "calDay.html",
controller: function($scope) {
// not sure what this is
}
};
});
指令模板
<div class="dayExercise" ng-repeat="item in day.array" ng-click="method()">
{{item}}
</div>
关于javascript - Angularjs ng-click() 不是从 ng-repeat 指令触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35563668/