Define a function for ng-click in an Angular directive

Don P

How can I attach a function to be used with ng-click in an Angular directive?

I have defined my click handler in the link function. Using this function in my directive's ng-click attribute does not run it.

Example:

I have an angular directive called "card". When "card" is clicked, I want to change its flipped attribute from false to true.

controller - has an array of cards

$scope.cards = [
  {id: 23, flipped: false},
  {id: 315, flipped: false},
  {id: 182, flipped: false}
];

directive - renders a card, and has a function to "flip" it.

myApp.directive('card', function(){
  return {
    scope: {
      card: "="
    },
    link: function(scope, elem, attrs) {
      // Create a function that will be called on click via ng-click.
      scope.flipCard = function(){
        console.log('fipped!');
        scope.card.flipped = true;
      }
    },
    template: "<div>I'm a card</div>"
  }
});

html - show all the cards

<div ng-repeat="card in cards">
  <card card="card" ng-click="flipCard()"></card>
</div>

When a card is clicked, the flipCard() function isn't being called. Why is this?

Note

I am aware of using bind to attach handlers in link. I am specifically asking why ng-click does not seem to have access to the directive's scope, as defined in link. Here is a solution that works with bind. I am looking for a solution that works with ng-click.

link: function(scope, elem, attrs) {
  elem.bind('click', function(e){
    scope.flipCard();
  });

  scope.flipCard = function(){
    console.log('tap!');
    scope.card.flipped = true;
  }; 
}
kozlice

The problem is that ng-click on your <card> element tries to call flipCard() from controller's scope, not directive's one.

I'd write something like this:

http://plnkr.co/edit/Jvum0F?p=preview

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Calling a method in parent controller from ng-click inside angular directive

분류에서Dev

Angular Click outside of an element in directive

분류에서Dev

Common directive ng-click guidance needed

분류에서Dev

How do I pass a function to an angular directive inside ng-repeat (arg is undefined)?

분류에서Dev

angular directive ng-repeat scope and Service

분류에서Dev

Problema with ng-click function

분류에서Dev

element.on('click') on a directive within ng-if doesn't work

분류에서Dev

angular directive - scope undefined inside function

분류에서Dev

Angular directive link function never runs

분류에서Dev

Angular.js call jquery function in Directive

분류에서Dev

Dynamic variable for angular ng-click attribute

분류에서Dev

Ionic / Angular ng-click for select input

분류에서Dev

Angular Directive needs access to variable in ng-repeat

분류에서Dev

Angular Directive captures initial null value of ng-model

분류에서Dev

how to toggle class in angular js on ng-click

분류에서Dev

Angular in chrome auto-firing ng-click

분류에서Dev

How to pass an AngularJs ng-click function on a button?

분류에서Dev

ng-click wont trigger, function not working. Could it be scope?

분류에서Dev

Angular select within a ng-repeat directive: how can I access the children scopes?

분류에서Dev

How can I pass a filter into an angular directive to be used in an ng-repeat?

분류에서Dev

Angular JS: How to call a function inside link from controller in angular directive

분류에서Dev

ng-repeat of a directive and $validators

분류에서Dev

Update data in angular directive

분류에서Dev

Angular directive modifying scope

분류에서Dev

angular order with directive

분류에서Dev

Angular Directive not for child element

분류에서Dev

Angular Directive not for child element

분류에서Dev

Angular directive checking active or not

분류에서Dev

Angular Directive는 ng-model의 초기 null 값을 캡처합니다.

Related 관련 기사

  1. 1

    Calling a method in parent controller from ng-click inside angular directive

  2. 2

    Angular Click outside of an element in directive

  3. 3

    Common directive ng-click guidance needed

  4. 4

    How do I pass a function to an angular directive inside ng-repeat (arg is undefined)?

  5. 5

    angular directive ng-repeat scope and Service

  6. 6

    Problema with ng-click function

  7. 7

    element.on('click') on a directive within ng-if doesn't work

  8. 8

    angular directive - scope undefined inside function

  9. 9

    Angular directive link function never runs

  10. 10

    Angular.js call jquery function in Directive

  11. 11

    Dynamic variable for angular ng-click attribute

  12. 12

    Ionic / Angular ng-click for select input

  13. 13

    Angular Directive needs access to variable in ng-repeat

  14. 14

    Angular Directive captures initial null value of ng-model

  15. 15

    how to toggle class in angular js on ng-click

  16. 16

    Angular in chrome auto-firing ng-click

  17. 17

    How to pass an AngularJs ng-click function on a button?

  18. 18

    ng-click wont trigger, function not working. Could it be scope?

  19. 19

    Angular select within a ng-repeat directive: how can I access the children scopes?

  20. 20

    How can I pass a filter into an angular directive to be used in an ng-repeat?

  21. 21

    Angular JS: How to call a function inside link from controller in angular directive

  22. 22

    ng-repeat of a directive and $validators

  23. 23

    Update data in angular directive

  24. 24

    Angular directive modifying scope

  25. 25

    angular order with directive

  26. 26

    Angular Directive not for child element

  27. 27

    Angular Directive not for child element

  28. 28

    Angular directive checking active or not

  29. 29

    Angular Directive는 ng-model의 초기 null 값을 캡처합니다.

뜨겁다태그

보관