Angularjs如何在触摸原始文件的情况下编辑$ scope中的变量对象的副本?

时间:2017-01-20 23:42:48

标签: 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,
          "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);
    });

1 个答案:

答案 0 :(得分:0)

angular.copy用于克隆AngularJS中的对象。所以你的代码可能就像:

newdata.push(angular.copy($scope.data[i]));