如何在嵌套的ng-repeats中获取索引

run*_*ero 7 angularjs

我在ng-repeat中有一个ng-repeat.我想在两个级别获得索引,但我无法:

<tbody>
    <tr ng-repeat="element in body">
      <td ng-repeat="h in header" style="width:{{h.width}}px">
        <div col="{{$parent.$parent.$index}}" row="{{$parent.$index}}">{{element[h.column]}}</div>
      <td>
    </tr>
  </tbody>  
Run Code Online (Sandbox Code Playgroud)

这里也是一个plnkr. http://plnkr.co/edit/dGoN43HzQefVpCsp2R3j

问题是"col"的值永远不会被填充.它不捕获索引.谁能帮助

Poy*_*maz 12

你不必使用$parent,只是$index用来获取内部循环的索引并使用数组的indexOf()函数来获取外部循环的索引 ...

HTML

<tr ng-repeat="element in body">
  <td ng-repeat="h in header" style="width:{{h.width}}px">
    <div col="{{body.indexOf(element)}}" row="{{$index}}">{{element[h.column]}}</div>
  <td>
</tr>
Run Code Online (Sandbox Code Playgroud)

UPDATE

实际上使用ng-init会更好地得到外循环的索引 ...在这里你可以在每一个转弯处轻松设置rowIndex当前索引并在内循环中使用它...

  <tbody>
    <tr ng-repeat="element in body" ng-init="rowIndex = $index">
      <td ng-repeat="h in header" style="width:{{h.width}}px">
        <div col="{{$index}}" row="{{rowIndex}}">{{element[h.column]}}</div>
      <td>
    </tr>
  </tbody> 
Run Code Online (Sandbox Code Playgroud)

这里是更新的PLUNKER