How can I copy an object and lose its reference in Angular?
With AngularJS, I can use angular.copy(object)
, but I\'m getting some error using that in An
Till we have a better solution, you can use the following:
duplicateObject = JSON.parse(JSON.stringify(originalObject));
EDIT: Clarification
Please note: The above solution was only meant to be a quick-fix one liner, provided at a time when Angular 2 was under active development. My hope was we might eventually get an equivalent of angular.copy()
. Therefore I did not want to write or import a deep-cloning library.
This method also has issues with parsing date properties (it will become a string).
Please do not use this method in production apps. Use it only in your experimental projects - the ones you are doing for learning Angular 2.