nicker nicker - 5 months ago 19
AngularJS Question

ng-repeat how to get selected data index from script side

I would like to get the index position of selected data in the script side

<table ng-table="testTable">
<tbody>
<tr ng-repeat="data in $data">
<td data-title="data">
{{data.name}}
< /td>
<td data - title="'Actions'">
<button ng-click="test(data)">Add</button>
</td>
</tr>
</tbody>
</table>


something like that

$scope.test = function(data){
var x = data.index;
}


I know on the html I could do this

<button ng-click="test(data,$index)">Add</button>


and pass the $index to the script side, but i'm sure there's other way.

Could have some help here,
Thanks.

Answer

May be:

  <tr ng-repeat="data in $data" ng-init="data.index = $index;">
      <td data-title="data"> 
          {{data.name}} 
      </td>
      <td data - title="'Actions'">
           <button ng-click="test(data)">Add</button>
      </td>
  </tr>

Edit: see working example: http://plnkr.co/edit/87PT7pvIKZNe1DyQ8uUu?p=preview