加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS:ng-repeat track by $index在嵌套循环中

发布时间:2020-12-17 08:28:31 所属栏目:安全 来源:网络整理
导读:我需要第二个$ index我的嵌套的ng-repeat循环。如何和在哪里放它? AngularJS site说 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 b
我需要第二个$ index我的嵌套的ng-repeat循环。如何和在哪里放它?

AngularJS site说

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?-->

如果我再次使用$ index,它似乎工作,但我不知道这是正确的事情吗?
我相信有一个简单而正确的做法,我只是不能找到一个例子。

谢谢

$ index将引用最内层ngRepeat范围上的索引,所以如果这是你需要的,你可以使用它。

文档描述的是一个场景,其中您需要访问在父ngRepeat中的$ index。你可以通过几种方式得到它:一个是使用$ parent,另一个是使用ngInit,如Angular docs建议的。这里有一个例子…

<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>

Fiddle

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读