如何在AngularJS中使用' splice '方法之前将值从一个数组推送到另一个数组

How to push values from an array to another array before using `splice` method in AngularJS?

本文关键字:数组 另一个 一个 AngularJS 方法 splice      更新时间:2023-09-26

我有一个AngularJS方法从array的中间到splice元素,但我想在使用splice方法之前将要删除的元素添加到另一个数组。这是我的代码

$scope.method = function (index) {
            $scope.array2.push($scope.array1[index]);
            $scope.array1.splice(index, 1);
        }

当我调用方法时,array1[index]值可以被检索,但它们不会被推入array2

这是我的HTML代码

<div data-ng-repeat="item in array1">
    <button class="btn btn-success pull-right" data-ng-click="method($index)">
        <i class="fa fa-trash" aria-hidden="true"></i>
    </button>
    <br />
</div>

查看函数拼接它将返回已删除的值;

$scope.method = function (index) {
     if(! $scope.array2 ) $scope.array2 = [];
     $scope.array2 = $scope.array2.concat($scope.array1.splice(index, 1););
}

我只修改这部分代码。

$scope.method = function (index) {
        $scope.array2.push($scope.array1.slice(index, 1));
        $scope.array1.splice(index, 1);
};

查看这个jsfiddle示例