在嵌套的 ng-repeat 中传递 2 个 $index 值 [英] passing 2 $index values within nested ng-repeat
问题描述
所以我有一个嵌套在另一个 ng-repeat 中的 ng-repeat 以构建导航菜单.在内部 ng-repeat 循环的每个 上,我设置了一个 ng-click,它通过传入 $index 让应用程序知道我们需要哪个来调用该菜单项的相关控制器.但是,我还需要从外部 ng-repeat 传入 $index,以便应用程序知道我们在哪个部分以及哪个教程.
So I have an ng-repeat nested within another ng-repeat in order to build a nav menu. On each <li>
on the inner ng-repeat loop I set an ng-click which calls the relevant controller for that menu item by passing in the $index to let the app know which one we need. However I need to also pass in the $index from the outer ng-repeat so the app knows which section we are in as well as which tutorial.
<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>
这是一个 Plunker http://plnkr.co/edit/bJUhI9oGEQIql9tahIJN?p=preview
here's a Plunker http://plnkr.co/edit/bJUhI9oGEQIql9tahIJN?p=preview
推荐答案
每个 ng-repeat 使用传递的数据创建一个子作用域,并在该作用域中添加一个额外的 $index
变量.
Each ng-repeat creates a child scope with the passed data, and also adds an additional $index
variable in that scope.
所以你需要做的是到达父作用域,并使用那个$index
.
So what you need to do is reach up to the parent scope, and use that $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>
这篇关于在嵌套的 ng-repeat 中传递 2 个 $index 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!