簡體   English   中英

AngularJS ng-click()未從ng-repeat指令觸發

[英]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>

Demo Plunkr

您需要在指令的控制器中定義“測試”功能(寫在“ //不知道這是什么的位置”)

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

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

您的問題是$ scope問題。 您必須將函數傳遞給指令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM