Passing parameter to ng-click directive, within custom directive

后端 未结 2 2021
孤街浪徒
孤街浪徒 2020-12-31 06:45

I\'m using ng-repeat in the template for a custom directive as follows:

  • 2条回答
    •  慢半拍i
      慢半拍i (楼主)
      2020-12-31 07:30

      Figured this out. Hope it helps anyone else stuck on it.

      Firstly this

      ng-click="togglePhoto({{$index}})"
      

      Should be

      ng-click="togglePhoto($index)"
      

      Braces not needed!

      Secondly I found that you can pass the event object into the click function eg

      ng-click="togglePhoto($event)"
      

      Then catch that event and find out what element trigged it in your click function

      $scope.togglePhoto = function(e)
      {
          console.log(e.currentTarget)
      }
      

    提交回复
    热议问题