2

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?

1
  • @CodeNashor Hover over the words withing the calendar days. they turn blue. You should be able to click them to trigger the function. Commented Feb 22, 2016 at 20:59

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

You are really fast, but I am faster :D
1

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

1 Comment

or even better, as suggested in other answers, pass the reference to the function from parent scope
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.