AngularJS: ng-repeat track by $index inside nested loops

前端 未结 1 1256
-上瘾入骨i
-上瘾入骨i 2020-12-23 13:35

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 propertie

相关标签:
1条回答
  • 2020-12-23 14:12

    $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

    0 讨论(0)
提交回复
热议问题