如何在AngularJS中使用过滤器从数组中删除项目? [英] How to remove item from array with filter in AngularJS?

查看:24
本文介绍了如何在AngularJS中使用过滤器从数组中删除项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在没有任何过滤器的情况下点击 tr 时,我的函数 array.splice() 起作用了.数组中的索引顺序正确,因此 array.splice() 可以工作.

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.

当过滤器启用时,数组中的索引不会更新并且仍然保持相同的顺序.所以 array.splice() 删除了错误的项目.

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>
    </tr>

    $scope.remove = function($event,index,array){
        array.splice(index,1);
    };

如何更新数组中的索引?或者如何删除正确的项目?

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

推荐答案

最简单的解决方案是更改您的删除函数以接收项目而不是索引.

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){
            $scope.projects.splice(i,1);
        }
    }
}

Plunker 示例:http://plnkr.co/edit/51SNVMQjG3dsmpYI5RyY?p=preview

Example Plunker: http://plnkr.co/edit/51SNVMQjG3dsmpYI5RyY?p=preview

这篇关于如何在AngularJS中使用过滤器从数组中删除项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆