Nate May
Nate May

Reputation: 4062

Angularjs ng-click() is not firing from ng-repeat directive

I am trying to trigger test() using ng-click but it's not working. The test() function works with ng-click elsewhere though, so I am assuming that it has to do with the fact that it is an ng-repeated directive.

See in Plunker

How do I fix this?

Upvotes: 2

Views: 768

Answers (3)

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

Your directive is using isolated scope, so it don't have access to it parent scope.

You need to pass the method to directive from its isolated scope using &

<div ng-repeat="day in thisMonth track by $index">
    <drop-target index="$index" day="day" method="test()"></drop-target>
</div>

Directive

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
    }
  };
});

Directive template

<div class="dayExercise" ng-repeat="item in day.array" ng-click="method()">
  {{item}}
</div>

Demo Plunkr

Upvotes: 2

Frane Poljak
Frane Poljak

Reputation: 2365

You need to defint the "test" function in directive's controller (right where you wrote "//don't know what is this")

Upvotes: 1

Eigi
Eigi

Reputation: 716

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

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

Your problem is a $scope problem. You have to pass the function to your directive.

Upvotes: 0

Related Questions