I am trying to remove elements from the array $scope.items
so that items are removed in the view ng-repeat="item in items"
Just for demonstrative purposes here is some code:
for(i=0;i<$scope.items.length;i++){
if($scope.items[i].name == 'ted'){
$scope.items.shift();
}
}
I want to remove the 1st element from the view if there is the name ted right? It works fine, but the view reloads all the elements. Because all the array keys have shifted. This is creating unnecessary lag in the mobile app I am creating..
Anyone have an solutions to this problem?
You can use plain javascript - Array.prototype.filter()
If you have any function associated to list ,when you make the splice function, the association is deleted too. My solution:
The list param is named items. The param x.done indicate if the item will be deleted. Hope help you. Greetings.
Here is
filter
with Underscore library might help you, we remove item with name "ted"Just a slight expansion on the 'angular' solution. I wanted to exclude an item based on it's numeric id, so the ! approach doesn't work. The more general solution which should work for { name: 'ted' } or { id: 42 } is:
For anyone returning to this question. The correct "Angular Way" to remove items from an array is with $filter. Just inject $filter into your controller and do the following:
You don't need to load any additional libraries or resort to Javascript primitives.
Using the indexOf function was not cutting it on my collection of REST resources.
I had to create a function that retrieves the array index of a resource sitting in a collection of resources: