[英]Angularjs ng-click() is not firing from ng-repeat directive
我正在嘗試使用ng-click
觸發test()
但是它不起作用。 test()
函數可在其他地方使用ng-click
工作,因此我假設它與ng-repeat
ed指令有關。
我該如何解決?
您的指令正在使用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>
您需要在指令的控制器中定義“測試”功能(寫在“ //不知道這是什么的位置”)
更新: http : //plnkr.co/edit/mKnX6qpxQBy20JMssWHa?p=preview
scope: {
day: '=',
index : '=',
funcOnClick: '&'
}
您的問題是$ scope問題。 您必須將函數傳遞給指令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.