简体   繁体   English

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

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

I have a AngularJS method to splice elements from middle of an array but I want to add the elements to be deleted to another array before using splice method. 我有一个AngularJS方法来从array中间splice元素,但我想在使用splice方法之前将要删除的元素添加到另一个数组中。 Here is my code 这是我的代码

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

when I am calling the method array1[index] values could be retreived but they not getting pushed into array2 当我调用方法array1[index]值可以检索但它们没有被推入array2

here is my html code 这是我的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>

Look the function splice It will return your deleted value; 查看函数splice它将返回您删除的值;

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

I change only this part of your code. 我只更改您代码的这一部分。

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

Check this jsfiddle example 检查这个jsfiddle示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM