I have list of items created via ng-repeat
. I also have Delete button.
Clicking delete button removes last item of the array one by one. Plunker
But I want to remove items one by one starting from the first item. How can I do that? I used this for removing list Items:
$scope.index = 1;
$scope.remove = function(item) {
var index = $scope.cards.indexOf(item);
$scope.cards.splice(index, 1);
}
Is there any way I can remove from the top?
Plunker
Made changes to .. now it will remove from the top
Just use
arr.slice(startingIndex, endingIndex)
.If you do not specify the
endingIndex
, it returns all the items starting from the index provided.In your case
arr=arr.slice(1)
.The easiest way is using
shift()
. If you have an array, theshift
function shifts everything to the left.There is a function called
shift()
. It will remove the first element of your array.There is some good documentation and examples.