bryan bryan - 1 month ago 4
AngularJS Question

Changing duplicate $scope array but it's affecting original ng-repeat

I have an ng-repeat that is cycling through

$scope.myarray
.

I have the following function:

var copy = $scope.myarray;

$.each(copy, function(index, data){
data.name = "";
});


When I do this, it seems to be affecting my
ng-repeat
loop on
$scope.myarray
.

When I do:

console.log(copy);
console.log($scope.myarray);


They both seem to have the
$$hashKey
so I am thinking that is what is screwing things up.

What is the best way to manipulate my copy of the array without affecting the first version?

Answer

to copy you should use

var copy = angular.copy($scope.myarray);
Comments