我有一个AngularJS
方法来从数组
中间拼接
元素,但我想将要删除的元素添加到另一个数组中在使用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>
最佳答案
看函数splice 它将返回您删除的值;
$scope.method = function (index) {
if(! $scope.array2 ) $scope.array2 = [];
$scope.array2 = $scope.array2.concat($scope.array1.splice(index, 1););
}
关于javascript - 在 AngularJS 中使用 `splice` 方法之前,如何将值从一个数组推送到另一个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40001417/