add and remove items between arrays in angular
This question already has an answer here:
You can get index as second parameter of angular.forEach. Then us splice to remove the item from original array. Check the code below.
angular.forEach($scope.results, function (item, index) {
if (item.selected) {
$scope.list.push(item);
$scope.results.splice(index, 1);
};
});
I just realized there is a drawback in using splice inside angular.forEach loop that it will reindex the array after removing the item. So the immediate next item will be skipped if an item is removed.
So below would be the right solution.
var len = $scope.results.length;
while (len--) {
var item = $scope.results[len];
if (item.selected) {
$scope.list.push(item);
$scope.results.splice(len, 1);
};
}
Thanks.
链接地址: http://www.djcxy.com/p/3218.html上一篇: 从特定位置的数组中删除值
下一篇: 以角度添加和删除数组之间的项目