object.assign angular 2 is not working correctlly

时间:2016-10-20 18:47:29

标签: angular

I am using object.assign to create new copy of an object upon making any change in the new copy the changes are reflected in the original copy .but i don't want this

 let xyz = Object.assign({}, this.model);

property i changed is deep inside the model.

Is there any other alternative of object.assign ;as any changes in xyz reflects in original object .I know Object.assign works other way .but not sure what's wrong in my model

1 个答案:

答案 0 :(得分:3)

Shouldn't it just be something like this

let xyz = this.model;
xyz.obj1.prop1 = "Change";

The change will be reflected in both objects.

OR

If you want a copy but not the refernce to the original you can go like this.

let xyz = JSON.parse(JSON.stringify(this.model));
xyz.obj1.prop1 = "Change";

This will change ONLY the property in xyz and NOT in this.model