gpt4 book ai didi

javascript - Angularjs ng-click() 没有从 ng-repeat 指令触发

转载 作者:行者123 更新时间:2023-11-29 19:15:39 26 4
gpt4 key购买 nike

我正在尝试使用 ng-click 触发 test() 但它不起作用。 test() 函数在其他地方与 ng-click 一起使用,所以我假设它与它是一个 ng-repeat< 的事实有关ed 指令。

See in Plunker

我该如何解决这个问题?

最佳答案

您的指令正在使用 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>

Demo Plunkr

关于javascript - Angularjs ng-click() 没有从 ng-repeat 指令触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35563668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com