跟踪嵌套的ng-repeat索引的 [英] keeping track of nested ng-repeat index's
问题描述
所以我有一个嵌套的ng-repeat像这样:
So I have a nested ng-repeat like so:
<div ng-repeat="data in flow" ng-init="$flowIndex = $index">
Index: {{ $index }}
<div ng-click="flow.splice($index, 1)">Delete me</div>
<div ng-repeat="inside_data in flow[$flowIndex]">
Inside index: {{ $index }}
</div>
</div>
我希望能够删除我的 $ flowIndex
中的索引.但是,如果我有这样的事情:
I want to be able to delete index in my $flowIndex
. However if I have something like this:
0
1
2
3
然后删除索引 2
.如果我删除索引 3
,则找不到它,因为ng-init变量仍将其放在索引3处,但实际上不在索引2处.
And I delete index 2
. If I go and delete index 3
, it isn't found because ng-init variable still things its at index 3 but really its not at index 2.
有人知道可以解决吗?
推荐答案
您可以摆脱 $ flowIndex
,这不是必需的,您可以使用 $ parent.$ index
,相反,当您使用 ngRepeat
时,它将创建一个子作用域,而 $ index
是该作用域的一部分.还可以考虑将删除逻辑移到控制器中.
You can get rid of $flowIndex
, it's not necessary, you can use $parent.$index
instead, when you are using ngRepeat
it creates a child scope and $index
is part of that scope. Also consider moving your deleting logic into the controller.
控制器:
$scope.delete = function ($index) {
$scope.flow.splice($index, 1);
};
HTML:
<div ng-repeat="data in flow">
Index: {{ $index }}
<div ng-click="delete($index)">Delete me</div>
<div ng-repeat="inside_data in flow[$index]">
Inside index: {{ $parent.$index }} -> {{ $index }}
</div>
</div>
这篇关于跟踪嵌套的ng-repeat索引的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!