AngularJS:NG-重复曲目由嵌套的循环在$指数 [英] AngularJS: ng-repeat track by $index inside nested loops

查看:158
本文介绍了AngularJS:NG-重复曲目由嵌套的循环在$指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我的嵌套NG-重复循环第二$指数。如何,我应该把在那里?

AngularJS网站说:


  

这些属性创建别名是可能的ngInit。这个
  可能是有用的时候,比如嵌套ngRepeats。


 < D​​IV NG重复=人的人通过$指数跟踪>
    < D​​IV NG重复=东西在阵列由轨道$索引2> <! - 在哪里Init这和如何管理 - &GT?;

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

感谢


解决方案

$指数将参照该指数最里面的 ngRepeat 范围,所以如果这是你需要什么,你可以使用它。

什么Docs是描述的是你需要在父 ngRepeat 进入 $指数的场景。你可以得到它在几个方面:一个是使用 $父,另一个是使用 ngInit ,作为角文档建议。下面是一个例子...

 <李NG重复=的东西事NG-的init =parentIndex = $指数>
    {{$指数}}
    < UL>
        <李NG重复=,在thing.values​​价值>
            {{值}}
            {{$指数}}<! - 内$指数 - >
            {{$父$指数}}<! - 父$指数 - >
            {{parentIndex}}<! - 还父$指数 - >
        < /李>
    < / UL>
< /李>

小提琴

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>

Fiddle

这篇关于AngularJS:NG-重复曲目由嵌套的循环在$指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆