【问题标题】:Remove item from list after filtering过滤后从列表中删除项目
【发布时间】:2013-04-22 09:57:19
【问题描述】:

我有以下问题:

我创建了一个允许用户从列表中删除项目的列表,如下所示:

当用户点击垃圾桶图标时,该项目被正常删除。 问题是当用户使用顶部的过滤器时。

在这种情况下,如果我删除数字 6565(原始列表中的索引 4,过滤列表中的 1),删除的项目在原始列表的索引 1 上,导致删除编号为 #564456 的寄存器

这是我的点击删除调用:

 $scope.deleteOwn = function (uuid) {
    console.log(uuid);
    var coupon = $scope.ownsCoupons[uuid];
    Coupon.delete({'id' : coupon.uuid}, function () {
        $scope.ownsCoupons.splice(uuid, 1);
    });
}

这是我的 html 模板:

<td><a href="" ><i class="icon-trash" ng-click="deleteOwn($index)"></i></a></td>

我也尝试使用代码:$scope.ownsCoupons.splice(coupon, 1);但没有成功。

有人知道怎么解决吗?

我使用以下参考编码:AngularJS How to remove an Item from scope

[编辑]

我为此创建了一个 Plunker:http://plnkr.co/edit/Fhxp6uZyTJCY05CAQ7yA?p=preview

【问题讨论】:

  • 您不能中继索引,而是使用对象引用。不知道为什么 splice 不适合你。您需要共享更多代码(最好与 plunker 一起使用),以便人们可以为您提供更多帮助。
  • 感谢您的回复!我已经用 plunker 更新了问题。

标签: angularjs angularjs-ng-repeat


【解决方案1】:

正如@pkozlowski.opensource 所述,您不能依赖$index 以这种方式识别数组中的项目。我会做出以下改变:

HTML:

<td><a ng-click="deleteWish(coupon)"><i class="icon-trash"></i></a></td>

JS:

$scope.deleteWish = function (coupon) {
    var index = $scope.coupons.indexOf(coupon);
    if (index != -1) {
        $scope.coupons.splice(index, 1);
    }
}

这是一个正常工作的 Plunker:http://plnkr.co/edit/b0b2cYGsM5wtw8hIrQB5?p=preview

【讨论】:

  • 嘿布兰登,你能解释一下 (index != -1) 到底在做什么吗?谢谢。
  • @LoveAndHappiness array.indexOf(item) 如果在array 中找不到item,则返回-1。如果index != -1,则表示已找到该项目,可以将其删除。
猜你喜欢
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-20
  • 1970-01-01
  • 2019-01-26
  • 2016-05-08
  • 2011-03-18
相关资源
最近更新 更多