小编典典

在嵌套的ng-repeat中传递2个$ index值

angularjs

因此,我在另一个ng-repeat中嵌套了一个ng-repeat,以构建导航菜单。在<li>内部ng-repeat循环的每个循环上,我都设置了一个ng-
click,它通过传入$ index来调用该菜单项的相关控制器,以使应用知道我们需要哪个。但是,我还需要从外部ng-repeat传递$
index,以便应用程序知道我们在哪个部分以及哪个教程中。

<ul ng-repeat="section in sections">
    <li  class="section_title {{section.active}}" >
        {{section.name}}
    </li>
    <ul>
        <li class="tutorial_title {{tutorial.active}}" ng-click="loadFromMenu($index)" ng-repeat="tutorial in section.tutorials">
            {{tutorial.name}}
        </li>
    </ul>
</ul>

这是一个plnkrhttp://plnkr.co/edit/bJUhI9oGEQIql9tahIJN?p=preview


阅读 272

收藏
2020-07-04

共1个答案

小编典典

每个ng-repeat都会使用传递的数据创建一个子作用域,并$index在该作用域中添加一个附加变量。

因此,您需要做的是达到父级作用域并使用$index

看到http://plnkr.co/edit/FvVhirpoOF8TYnIVygE6?p=preview

<li class="tutorial_title {{tutorial.active}}" ng-click="loadFromMenu($parent.$index)" ng-repeat="tutorial in section.tutorials">
    {{tutorial.name}}
</li>
2020-07-04