[英]Remove items from Array skips certain items
I have the following page (simple): 我有以下页面(简单):
As you can see, at the top I have an input, then <ul>
and finally a button to save changes. 如您所见,在顶部,我有一个输入,然后是<ul>
,最后是一个保存更改的按钮。 My <ul>
is bound to a array of items
. 我的<ul>
绑定到一组items
。 Once user clicks Uloz zmeny (Save Changes) I am triggering ng-click="vm.SaveChanges()"
which looks like following: 用户单击Uloz zmeny(保存更改)后,我将触发ng-click="vm.SaveChanges()"
,如下所示:
vm.SaveChanges = function () {
angular.forEach(vm.items, function (value, key) {
if (value.toRemove == true) {
//remove item from the list
var iIndex = vm.items.indexOf(value);
vm.items.splice(iIndex, 1);
};
});
};
where vm
is defined as following at the beginning of my code: 在我的代码开头,其中vm
的定义如下:
(function () {
"use strict";
angular.module("app-shopping").controller("itemsController", itemsController);
function itemsController($http) {
var vm = this;
vm.items = [];.....more code after here
Every item under my '' has the following structure: “”下的每个项目都具有以下结构:
{
"id": 2,
"orderId": 2,
"text": "Item 2",
"toRemove": true
},
Finally, when user checks an item under the <li>
I am triggering vm.toggleCompleted()
which simply looks like this (it simply changes a boolean state of current item from true to false or vice versa): 最后,当用户检查<li>
下的项目时,我触发了vm.toggleCompleted()
,它看起来像这样(它只是将当前项目的布尔状态从true更改为false,反之亦然):
vm.toggleCompleted = function (sItem) {
sItem.toRemove = !sItem.toRemove;
};
Here comes the question: Why when I run this code it does not remove all checked items in the array? 问题来了:为什么当我运行此代码时,它不会删除数组中的所有选中项? For example in this specific case (see image above) it would only remove Item 2
and skip Item 3
. 例如,在这种特定情况下(参见上图),它将仅删除Item 2
并跳过Item 3
。 I believe that the problem is caused by the fact that when Item 2
is remove from the list, Item 3
takes the index of already existing Item 2
and therefore is skipped. 我认为该问题是由于以下事实造成的:从列表中删除Item 2
时, Item 3
取得了已经存在的Item 2
的索引,因此被跳过。 Is this assumption correct? 这个假设正确吗? If yes, how do I need to change the code to make this run? 如果是,我该如何更改代码才能运行?
PS Edit to my code as recommended: PS根据建议编辑到我的代码:
<li class="list-group-item" ng-repeat="sItem in vm.items">
<div class="checkbox checkbox-success">
<input id="ListItem{{$index}}" type="checkbox" placeholder="test placeholder" ng-model="sItem.toRemove" ng-click="sItem.toRemove=!sItem.toRemove" />
<label for="ListItem{{$index}}">{{sItem.text}}</label>
</div>
</li>
I have changed the code the following way and it is working now: 我已经按照以下方式更改了代码,并且现在可以正常工作:
vm.SaveChanges = function () {
for (var i = vm.items.length - 1; i > -1; i--)
{
if (vm.items[i].toRemove == true)
{
vm.items.splice(i, 1);
}
}
};
Instead of using toggleCompleted use below at the place of check-box input 而不是在复选框输入位置使用下面的toggleCompleted用法
<input type="checkbox" ng-model="item.toRemove" ng-click="item.toRemove=!item.toRemove" />
And Use your new saveChanges method .. this should work fine.. 并使用新的saveChanges方法..应该可以正常工作。
是的,要绕过此问题,只需还原数组遍历,因为这样可以保证删除期间未检查元素的位置不会发生变化。
So, you code at the end should be like that: 因此,最后的代码应如下所示:
Template: 模板:
<li class="list-group-item" ng-repeat="sItem in vm.items">
<div class="checkbox checkbox-success">
<input id="ListItem{{$index}}" type="checkbox" placeholder="test placeholder" ng-click="sItem.toRemove = !sItem.toRemove" />
<label for="ListItem{{$index}}">{{sItem.text}}</label>
</div>
</li>
<button class="btn btn-success" ng-click="SaveChanges()"> Save</button>
Controller: 控制器:
$scope.SaveChanges = function () {
for (var i = $scope.vm.items.length - 1; i > -1; i--){
if ($scope.vm.items[i].toRemove) {
$scope.vm.items.splice(i, 1);
};
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.