Here is the scenario with my component:
@Component({
selector: 'child'
})
export class ChildComponent {
@Input() childObject: ChildObject;
changeObject(newObject: ChildObject){
childObject = newObject;
}
}
After calling changeObject
, the changes are reflected in my ChildComponent
, but the parent component (ParentComponent
) which includes the ChildComponent
does not update accordingly.
For instance, if I have {{parent.childObject.name}}
in the template of my ParentComponent
, this value remains unchanged.
I attempted to use
childObject = JSON.parse(JSON.stringify(newObject));
without success.
It seems to be an issue related to object reference modification. As a solution, I created a method called copy(newObject: ChildObject)
in my ChildObject
class to copy the properties one by one. However, when I call it within the changeObject
method, I encounter the following error:
ERROR TypeError: _this.childObject.copy is not a function.
Update: Definition of ChildObject class
export class ChildObject {
constructor(
public name: string // , ...
) { }
copy(childObject: ChildObject) {
this.name = childObject.name;
// ...
}
}