嵌套的ng-repeat双层循环,内层如何获取外层的$index?

时间:2022-02-10 00:25:17

html代码:

<div>
<ul ng-repeat="row in table track by $index">
<li ng-repeat="col in row track by col.id">
<span ng-click="add(col, $index)">{{col.value}}</span>
</li>
</ul>
</div>

此时$index是内层循环的$index,那么如何获取外层的$index呢?有两种方法:

1、$parent.$index

2、用ng-init将外层循环进行保存

<div>
<ul ng-repeat="row in table track by $index" ng-init="outerIndex=$index">
<li ng-repeat="col in row track by col.id">
<span ng-click="add(col, outerIndex)">{{col.value}}</span>
</li>
</ul>
</div>