Search code examples
angulardata-bindingvariable-binding

Avoid Angular/TypeScript variable binding


I need to create a clone of my object, without creating it's reference. When I try to copy EquipmentClass, witch is my main object to it's clone, and if I change some property of EquipmentClass it will change my EquipmentClassCloneEdit also. And I don't want that to happen.

I tried to assign the value like this:

this.equipmentClassCloneEdit = Object.assign({}, this.equipmentClass);

this is my model:

export class EquipmentClass {
    equipmentClassId: number;
    name: string;
    description: string;
    isDeleted: boolean;
    propertyValuesList: EquipmentClassPropertyValue[] = [];
}

Solution

  • Try this.equipmentClassCloneEdit = JSON.parse(JSON.stringify(this.equipmentClass))

    Object.assign() creates a shallow copy, so it wouldn't work on non-primitives like your propertyValuesList array.