我刚才意识到,我使用变量$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);
});
答案 0 :(得分:0)
angular.copy
用于克隆AngularJS中的对象。所以你的代码可能就像:
newdata.push(angular.copy($scope.data[i]));