Angularjs如何编辑$scope中变量对象的副本,使其不接触原始对象?
我刚刚意识到,我对变量Angularjs如何编辑$scope中变量对象的副本,使其不接触原始对象?,angularjs,Angularjs,我刚刚意识到,我对变量$scope.ratings所做的任何更改都会影响$scope.data,我不希望这样 $scope.data = [ { "0":"1", "1":"Creativity", "2":"5", "3":"1", "4":"2017-01-17 21:24:42", "5":"ratings", "6":null,
$scope.ratings
所做的任何更改都会影响$scope.data
,我不希望这样
$scope.data = [ {
"0":"1",
"1":"Creativity",
"2":"5",
"3":"1",
"4":"2017-01-17 21:24:42",
"5":"ratings",
"6":null,
"ID":"1",
"Title":"Creativity",
"Value":"5",
"Parent":"18",
"Timestamp":"2017-01-17 21:24:42",
"Category":"ratings",
"Author":18
}
]
var myid = 18;
var newdata = [];
for( var i in $scope.data ){
if($scope.data[i].Author == myid) {
//alert($scope.data[i].ID);
newdata.push($scope.data[i]);
}
$scope.ratings_temp= newdata;
$scope.ratings = newdata;
}
$scope.ratings[0].ID = 'loool';
console.log($scope.ratings);
console.log($scope.data);
});
angular.copy
用于克隆AngularJS中的对象。因此,您的代码可能如下所示:
newdata.push(angular.copy($scope.data[i]));