LoginSignup
18
18

More than 5 years have passed since last update.

angularjsのrepeatで、ng-clickで呼び出すメソッドにindexを渡す

Posted at

サンプル

<div ng-controller="SampleCtrl" data-ng-init="init()">
  <table>
    <tr ng-repeat="item in items">
      <td ng-click="methodA($index)">
        {{item.id}}
      </td>
      <td>{{item.firstName}}</td>
      <td>{{item.lastName}}</td>
    </tr>
  </table>
</div>
app.controller('SampleCtrl', function($scope) {
  $scope.items = [];
  $scope.init = function() {
     $scope.items = []; // TODO itemsに値を詰める
  };
  $scope.methodA = function(index) {
      alert($scope.items[index]);
  };
});

ng-click="methodA({{$index}})"とやってしまうと、エラーになるので注意

18
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
18