Here is the code
<tr ng-repeat="collection in collections | orderBy:'-modifiedDate' track by $index" ng-init="listIndex = $index">
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.
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.
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)"
This is how I got it to work.
in the template
in the controller
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[2] would still be { name: '3rd element', id: '3' }
Here's what you can do, you can give a name to that sorted collection created by angular :
And then you could use $scope.sortedCollections[2] instead
alert
listIndex
in thedeleteRow
method, if it is coming correct as 7, usesplice(listIndex,1)
to do the deletion.Or, even more simple:
And in your html: