Angularjs ng-click()不是从ng-repeat指令触发的

时间:2016-02-22 20:56:26

标签: javascript angularjs angularjs-directive angularjs-ng-repeat angularjs-ng-click

我正在尝试使用test()触发ng-click,但它无效。 test()函数在其他地方使用ng-click,所以我假设它与ng-repeat ed指令有关。

See in Plunker

如何解决此问题?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

您需要在指令的控制器中定义“测试”功能(在您写“//不知道这是什么”的地方)

答案 2 :(得分:0)

更新:http://plnkr.co/edit/mKnX6qpxQBy20JMssWHa?p=preview

scope: {
        day: '=',
        index : '=',
        funcOnClick: '&'
      }

您的问题是$ scope问题。您必须将该函数传递给您的指令。

相关问题