1

When I am trying to copy a new object to an old object via angular.copy, its reference gets changed. If I use = or JSON.parse(JSON.stringify(newObj)), the view is not updated with new value.

How can I solve this?

Code example:

$scope.value = newValue.ref;

View is not updated in this instance.

$scope.value = angular.copy(newValue.ref);

View is updated, but reference is changed.

1
  • 1
    Show your html file also. Commented Mar 9, 2017 at 7:10

1 Answer 1

1

To preserve the reference, use the two argument form of angular.copy:

//$scope.value = angular.copy(newValue.ref);

//USE two argument signature
angular.copy(newValue.ref, $scope.value);

This way the original reference is preserved.

Usage

angular.copy(source, [destination]);

Creates a deep copy of source, which should be an object or an array.

  • if a destination is provided, all of its elements (for arrays) or properties (for objects) are deleted and then all elements/properties from the source are copied to it.

— AngularJS angular.copy API Reference

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.