Angularjs ng-click() 不是从 ng-repeat 指令触发的
Angularjs ng-click() is not firing from ng-repeat directive
我正在尝试使用 ng-click
触发 test()
,但它不起作用。 test()
函数在其他地方与 ng-click
一起使用,所以我假设它与它是一个 ng-repeat
ed 指令这一事实有关。
我该如何解决这个问题?
更新:http://plnkr.co/edit/mKnX6qpxQBy20JMssWHa?p=preview
scope: {
day: '=',
index : '=',
funcOnClick: '&'
}
您的问题是 $scope 问题。您必须将函数传递给您的指令。
您的指令正在使用 isolated
作用域,因此它无法访问它的父作用域。
您需要使用 &
将方法从其独立范围传递给指令
<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>
您需要在指令控制器中定义 "test" 函数(就在您写“//不知道这是什么”的地方)
我正在尝试使用 ng-click
触发 test()
,但它不起作用。 test()
函数在其他地方与 ng-click
一起使用,所以我假设它与它是一个 ng-repeat
ed 指令这一事实有关。
我该如何解决这个问题?
更新:http://plnkr.co/edit/mKnX6qpxQBy20JMssWHa?p=preview
scope: {
day: '=',
index : '=',
funcOnClick: '&'
}
您的问题是 $scope 问题。您必须将函数传递给您的指令。
您的指令正在使用 isolated
作用域,因此它无法访问它的父作用域。
您需要使用 &
<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>
您需要在指令控制器中定义 "test" 函数(就在您写“//不知道这是什么”的地方)