[英]AngularJS: ng-repeat track by $index inside nested loops
I need a second $index for my nested ng-repeat loop. 我的嵌套ng-repeat循环需要第二个$ index。 How and where should I put it?
我该怎么做,在哪里放?
AngularJS site says AngularJS网站说
Creating aliases for these properties is possible with ngInit.
使用ngInit可以为这些属性创建别名。 This may be useful when, for instance, nesting ngRepeats.
例如,当嵌套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? 如果我再次使用$ index,它似乎有用,但我不确定这是正确的吗? I am sure there is an easy and correct way of doing it, I just wasn't able to find an example.
我确信有一种简单而正确的方法,我只是无法找到一个例子。
Thanks 谢谢
$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
. 文档描述的是您需要访问父
ngRepeat
中的$index
的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. 您可以通过以下两种方式获得它:一种是使用
$parent
,另一种是使用ngInit
,正如Angular文档所建议的那样。 Here's an example... 这是一个例子......
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.