如何让孩子纳克重复内的父范围阵列/ NG重复的索引 [英] How to get the index of an parent scope array/ng-repeat inside child ng-repeat
问题描述
我建立一个表,我有两个 NG-重复
我的表。
I am building a table and I have two ng-repeat
for my table.
我的问题是,如果有可能的的子NG重复
可以得到父 NG-重复
索引中。例如:
My question is if it's possible the child of the ng-repeat
can get the parent ng-repeat
's index. For example:
<tbody>
<tr ng-repeat="company in companies">
<td ng-repeat="product in company">
company # = {{the company index}} and product {{$index}}
</td>
</tr>
</tbody>
我不知道如何将TD NG-重复下添加公司指数
。有没有办法做到这一点?非常感谢!
I am not sure how to add the company index under the td ng-repeat
. Is there a way to do this? Thanks a lot!
推荐答案
这也正是 NG-INIT
进入画面,别名NG重复,恩的特殊属性: NG-的init =companyIdx = $指数
。因此,通过公司独创每个子范围 NG-重复
都会有这样的 companyIdx
属性。
This is exactly where ng-init
comes into picture, to alias special properties of ng-repeat, ex: ng-init="companyIdx=$index"
. So each child scope created by company ng-repeat
will have this companyIdx
property.
<tbody>
<tr ng-repeat="company in companies" ng-init="companyIdx=$index">
<td ng-repeat="product in company">
company # = {{companyIdx}} and product {{$index}}
</td>
</tr>
</tbody>
使用 $父
是好的,但你必须在它们之间创建一个子范围的其他任何指令,例如: - NG-如果
,另一个 NG-重复
等等。你必须去疯狂做 $母公司。母公司$ ....
。与 NG-INIT
走样使得清洁,更具可读性和可维护性以及
Using a $parent
is fine but it you have any other directive that creates a child scope in between them, eg:- ng-if
, another ng-repeat
etc.. you will have to go crazy doing $parent.$parent....
. Aliasing with ng-init
makes it clean and more readable and maintainable as well.
这篇关于如何让孩子纳克重复内的父范围阵列/ NG重复的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!