1

I have two variables. But updating 1 causes the other to update too. Can anyone explain what is happening here?

$scope.pages = [];
$scope.pagesSave = [];

var functionName = function(){

  $service.doThing1().then(function(res1){
    $scope.pagesSave = res1;
    console.log(res1)
    $service.doThing2().then(function(res2){
      $scope.pages = $service.formatThings(res2, res1);
      console.log($scope.pages)

  }, function(err){});
}, function(err){});

$scope.pages should be the final product

$scope.pagesSave should be the pre-formatted data

however, console logging these variables show them as being identical.

More oddly, console logging immediately after doing $service.doThing1() shows "res1" as having the correct length but only containing the data from $scope.pages, as seen in this image.

2 are missing even though the formatting function hasn't happened yet

enter image description here

Anyone know whats happening here?

I'm using Chrome for console logs if that helps

1

1 Answer 1

1

I think that inside your formatThings you send res1 as a result of your function which means $scope.pagesSave will equal res1 which will be equal to $scope.pages.

So the solution is to use angular.copy() to create a deep copy of your res1 and use the created object to clean it up. Can you share your formatThings?

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Tried this and had a look through the docs / examples for angular.copy(). This is what I needed!
You are welcome. Consider marking the question as answered ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.