在复制对象的值的时候,往往不能直接“=”,这样会造成引用赋值,应该利用一些函数进行对象的复制值。如下:
$scope.updateDeliveryOrder = function(wayPointsOrder) {
var tempDeListInfo = Object.assign({}, $scope.deListInfo);
var index = 1 ;
for( var i = 0; i < wayPointsOrder.length; i++ ){
$scope.deListInfo[i] = tempDeListInfo[wayPointsOrder[i]];
$scope.deListInfo[i].order = $scope.beginOrder + index;
$scope.deListInfo[i].indexImg = "images/seq/seq_"+index+".png";
$scope.makerLocation($scope.deListInfo[i], index);
index ++;
}
$scope.$apply();//强制更新数据
}
Object.assign()
方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }
console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }