What's alternative to angular.copy in Angular

后端 未结 15 1161
忘掉有多难
忘掉有多难 2020-11-29 00:43

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

15条回答
  •  抹茶落季
    2020-11-29 01:24

    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.

提交回复
热议问题