How to remove item from array with filter in AngularJS?

All we need is an easy explanation of the problem, so here it is.

When I click on tr without any filter, my function array.splice() works. Indexes in the array are in the correct order, so the array.splice() works.

When the filter is enable, Indexes in the array are not updated and still in the same order. So array.splice() removes the wrong item.

    <span ng-click="orderP0 = 'statut_name'; reversePO=!reversePO">order</span>

    <tr ng-repeat="project in projects | orderBy : orderPO : reverse track by $index" ng-click="remove($event,$index,projects)">
        <span class="label" ng-bind="project.statut_name"></span>

    $scope.remove = function($event,index,array){

How to update index in the array ? Or How to removes the right item ?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

It’s easier to splice you projects in the actual position of the element in the array using indexOf.

$scope.remove = function(project){

This way you need to pass to the remove function only the current project.

<tr ng-repeat="project in projects | orderBy : orderPO : reverse track by $index" ng-click="remove(project)">
    <span class="label" ng-bind="project.statut_name"></span>

Method 2

The simplest solution would be to change your remove function to take in the project instead of the index.

$scope.remove = function(project){
    for(var i = $scope.projects.length - 1; i >= 0; i--){
        if($scope.projects[i].statut_name == project.statut_name){

Example Plunker:

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply