Javascript 从子 ng-repeat 访问父 ng-repeat 的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14807258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access index of the parent ng-repeat from child ng-repeat
提问by Coder1
I want to use the index of the parent list (foos) as an argument to a function call in the child list (foos.bars).
我想使用父列表 (foos) 的索引作为子列表 (foos.bars) 中函数调用的参数。
I found a post where someone recommends using $parent.$index, but $indexis not a property of $parent.
我发现有人推荐使用 $parent.$index 的帖子,但$index不是$parent.
How can I access the index of the parent ng-repeat?
如何访问父项的索引ng-repeat?
<div ng-repeat="f in foos">
<div>
<div ng-repeat="b in foos.bars">
<a ng-click="addSomething($parent.$index)">Add Something</a>
</div>
</div>
</div>
回答by Coder1
My example code was correct and the issue was something else in my actual code. Still, I know it was difficult to find examples of this so I'm answering it in case someone else is looking.
我的示例代码是正确的,问题出在我的实际代码中。尽管如此,我知道很难找到这样的例子,所以我会回答它以防其他人正在寻找。
<div ng-repeat="f in foos">
<div>
<div ng-repeat="b in foos.bars">
<a ng-click="addSomething($parent.$index)">Add Something</a>
</div>
</div>
</div>
回答by Samuli Ulmanen
According to ng-repeat docs http://docs.angularjs.org/api/ng.directive:ngRepeat, you can store the key or array index in the variable of your choice. (indexVar, valueVar) in values
根据 ng-repeat docs http://docs.angularjs.org/api/ng.directive:ngRepeat,您可以将键或数组索引存储在您选择的变量中。(indexVar, valueVar) in values
so you can write
所以你可以写
<div ng-repeat="(fIndex, f) in foos">
<div>
<div ng-repeat="b in foos.bars">
<a ng-click="addSomething(fIndex)">Add Something</a>
</div>
</div>
</div>
One level up is still quite clean with $parent.$index but several parents up, things can get messy.
$parent.$index 上一级仍然很干净,但是有几个父级,事情可能会变得混乱。
Note: $indexwill continue to be defined at each scope, it is not replaced by fIndex.
注意:$index会在每个作用域继续定义,不会被fIndex.
回答by vucalur
Take a look at my answer to a similar question.
By aliasing $indexwe do not have to write crazy stuff like $parent.$parent.$index.
看看我对类似问题的回答。
通过别名,$index我们不必编写像$parent.$parent.$index.
Way more elegant solution whan $parent.$indexis using ng-init:
$parent.$index使用ng-init 的更优雅的解决方案:
<ul ng-repeat="section in sections" ng-init="sectionIndex = $index">
<li class="section_title {{section.active}}" >
{{section.name}}
</li>
<ul>
<li class="tutorial_title {{tutorial.active}}" ng-click="loadFromMenu(sectionIndex)" ng-repeat="tutorial in section.tutorials">
{{tutorial.name}}
</li>
</ul>
</ul>
Plunker: http://plnkr.co/edit/knwGEnOsAWLhLieKVItS?p=info
Plunker:http://plnkr.co/edit/knwGEnOsAWLhLieKVItS?p=info
回答by AkRoy
You can also get control of grand parent index by the following code
您还可以通过以下代码获得对祖父索引的控制
$parent.$parent.$index
回答by Vivek Panday
You can simply use use $parent.$index .where parent will represent object of parent repeating object .
您可以简单地使用 use $parent.$index .where parent 将代表父重复对象的对象。
回答by Gajanan Prasad
$parent doesn't work if there are multiple parents. instead of that we can define a parent index variable in init and use it
如果有多个父母,$parent 不起作用。相反,我们可以在 init 中定义一个父索引变量并使用它
<div data-ng-init="parentIndex = $index" ng-repeat="f in foos">
<div>
<div data-ng-init="childIndex = $index" ng-repeat="b in foos.bars">
<a ng-click="addSomething(parentIndex)">Add Something</a>
</div>
</div>
</div>

