I need a second $index for my nested ng-repeat loop. How and where should I put it?
我需要第二个$索引用于嵌套的ng-repeat循环。我该怎么把它放在哪里?
AngularJS site says
AngularJS网站上说
Creating aliases for these properties is possible with ngInit. This may be useful when, for instance, nesting ngRepeats.
使用ngInit可以为这些属性创建别名。例如,当嵌套ngRepeats时,这可能是有用的。
<div ng-repeat="person in persons track by $index">
<div ng-repeat="something in array track by $index2"> <!-- where to init this and how to manage it?-->
If I use $index again, it seems to work but I am not sure this is the right thing? I am sure there is an easy and correct way of doing it, I just wasn't able to find an example.
如果我再次使用$index,它似乎是有效的,但是我不确定这是正确的吗?我确信有一种简单而正确的方法,我只是找不到一个例子。
Thanks
谢谢
1 个解决方案
#1
115
$index
will refer to the index on the innermost ngRepeat
scope, so if that's what you need, you can just use it.
$index将引用最内层的ngRepeat范围内的索引,因此如果您需要的话,您可以使用它。
What the docs is describing is a scenario where you need access to $index
in the parent ngRepeat
. You can get it in a couple of ways: One is to use $parent
, and another is to use ngInit
, as the Angular docs suggested. Here's an example...
文档描述的是一个场景,您需要在父ngRepeat中访问$index。您可以通过两种方式获得它:一种是使用$parent,另一种是使用ngInit,正如棱角分明的文档所建议的那样。这里有一个例子……
<li ng-repeat="thing in things" ng-init="parentIndex = $index">
{{ $index }}
<ul>
<li ng-repeat="value in thing.values">
{{ value }}
{{ $index }} <!-- inner $index -->
{{ $parent.$index }} <!-- parent $index -->
{{ parentIndex }} <!-- also parent $index -->
</li>
</ul>
</li>
小提琴
#1
115
$index
will refer to the index on the innermost ngRepeat
scope, so if that's what you need, you can just use it.
$index将引用最内层的ngRepeat范围内的索引,因此如果您需要的话,您可以使用它。
What the docs is describing is a scenario where you need access to $index
in the parent ngRepeat
. You can get it in a couple of ways: One is to use $parent
, and another is to use ngInit
, as the Angular docs suggested. Here's an example...
文档描述的是一个场景,您需要在父ngRepeat中访问$index。您可以通过两种方式获得它:一种是使用$parent,另一种是使用ngInit,正如棱角分明的文档所建议的那样。这里有一个例子……
<li ng-repeat="thing in things" ng-init="parentIndex = $index">
{{ $index }}
<ul>
<li ng-repeat="value in thing.values">
{{ value }}
{{ $index }} <!-- inner $index -->
{{ $parent.$index }} <!-- parent $index -->
{{ parentIndex }} <!-- also parent $index -->
</li>
</ul>
</li>
小提琴