AngularJS:嵌套循环内 $index 的 ng-repeat 跟踪 [英] AngularJS: ng-repeat track by $index inside nested loops
问题描述
我的嵌套 ng-repeat 循环需要第二个 $index.我应该如何以及在哪里放置它?
<块引用>可以使用 ngInit 为这些属性创建别名.这个例如,在嵌套 ngRepeats 时可能很有用.
<div ng-repeat="$index2 的数组轨道中的东西"><!-- 在哪里初始化它以及如何管理它?-->如果我再次使用 $index,它似乎可以工作,但我不确定这是正确的吗?我相信有一种简单而正确的方法可以做到,只是找不到示例.
谢谢
解决方案 $index
将引用最内层 ngRepeat
作用域上的索引,所以如果这是你需要的,你可以使用它.
文档描述的是您需要访问父 ngRepeat
中的 $index
的场景.您可以通过多种方式获得它:一种是使用 $parent
,另一种是使用 ngInit
,正如 Angular 文档所建议的那样.这是一个例子...
{{ $index }}<ul><li ng-repeat="thing.values 中的值">{{ 价值 }}{{ $index }} {{ $parent.$index }} <!-- 父 $index -->{{ parentIndex }} <!-- 也是父 $index -->
I need a second $index for my nested ng-repeat loop. How and where should I put it?
AngularJS site says
Creating aliases for these properties is possible with ngInit. This
may be useful when, for instance, nesting 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.
Thanks
解决方案 $index
will refer to the index on the innermost ngRepeat
scope, so if that's what you need, you can just use it.
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...
<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>
这篇关于AngularJS:嵌套循环内 $index 的 ng-repeat 跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文