问题描述
这是代码
<tr ng-repeat="collection in collections | orderBy:'-modifiedDate' track by $index" ng-init="listIndex = $index">
如果删除orderBy:'-modifiedDate'
,则删除特定元素的效果很好.但是,我需要以排序的方式呈现集合/数组,这就是为什么我有orderBy的原因.
If I remove orderBy:'-modifiedDate'
, the deletion on a specific element is working great. However, I need the collection/array to be rendered in sorted way that's why I have orderBy.
如果我没有从代码中删除orderBy:'-modifiedDate'
,而是删除了第7位的随机元素,那么被删除的元素将永远是最后一个.
If I don't delete the orderBy:'-modifiedDate'
from the code and I delete a random element say on position 7, the element that gets deleted is the very last always.
我必须使用ng-init
,因为在上面显示的ng-repeat
中还有另一个ng-repeat
.我这样叫删除功能,ng-click="deleteRow(listIndex)"
I had to use ng-init
since I have another ng-repeat
inside the ng-repeat
shown above. I call the delete function like this, ng-click="deleteRow(listIndex)"
推荐答案
使用orderBy时,angular在范围内创建另一个集合",并更改其元素的顺序,但原始的集合"保持不变,例如:
When you use orderBy, angular creates another "collections" in the scope, and changes the order of its elements, but the original "collections" stays the same, for example if :
$scope.collections = [
{ name: '1st element', id: '1' },
{ name: '2nd element', id: '2' },
{ name: '3rd element', id: '3' }
];
<tr ng-repeat="collection in collections | orderBy:'id':true track by $index">
$ scope.collections [2] 仍为 {名称:'3rd element',id:'3'}
这就是您可以做的,您可以为由angular创建的排序后的集合命名:
Here's what you can do, you can give a name to that sorted collection created by angular :
<tr ng-repeat="collection in sortedCollections = (collections | orderBy:'id':true) track by $index">
然后您可以使用 $ scope.sortedCollections [2]
这篇关于删除带有orderBy的ng-repeat中的元素并通过失败跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!