考虑以下嵌套ng-repeat
指令:
<tr ng-repeat="r in SomeExpr1">
<td ng-repeat="c in SomeExpr2">
<p>c index is {{$index}}, r index is {{???}}</p>
</td>
</tr>
如何访问外部$index
的{{1}}(即内部ng-repeat
范围ng-repeat
隐藏的父作用域中的$index
?< / p>
答案 0 :(得分:17)
@Randal Schwartz在this post $parent
中指出了这一点。
<div ng-controller='demo-ctrl'>
<div ng-repeat="row in ctrl.matrix">
<div ng-repeat="column in row">
<span>outer: {{$parent.$index}} inner: {{$index}}</span>
</div>
</div>
</div>
答案 1 :(得分:5)
你需要来自Angular.js的ng-init
。可悲的是,这还没有移植过,但是如果有效的话会看起来像这样:
<script>
function Ctrl($scope) {
$scope.list = [['a', 'b'], ['c', 'd']];
}
</script>
<div ng-controller="Ctrl">
<div ng-repeat="innerList in list" ng-init="outerIndex = $index">
<div ng-repeat="value in innerList" ng-init="innerIndex = $index">
<span class="example-init">list[ {{outerIndex}} ][ {{innerIndex}} ] = {{value}};</span>
</div>
</div>
</div>