AngularJS

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

More than 5 years have passed since last update.


サンプル

<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}})"とやってしまうと、エラーになるので注意