In my Angular 16 app, i have a parent component that pass myObj (a plain javascript object) to children component that is treated as a model<MyObj>
<!-- parent.component.html -->
<app-children [myObjModel]="myObj"
(myObjModelChange)="doSomething($event)">
</app-children>
In my child component i handle myObj as a model input:
export class ChildrenComponent {
myObjModel = model<MyObj>();
onMyModelChange(event: MyObj) {
this.myObjModel.set(event);
doThings(event)
}
doThings(event: MyObj) {
console.log("I changed value in: " + event);
}
}
It works fine as i modify the value in my children component, it trigger the doThings() function.
What if i want to react in children component if the value change programmatically from father component? (like i load a configuration in father and pass myObj to my children that should trigger a function)
If myObj in father component change, the doThings() function is not called and even if i subscribe to change of myObjModel (with .subscribe() API) it doesn’t trigger.
I tried this in children component with no success:
constructor() {
this.myObjModel.subscribe((val) => {
doThings(val);
});
}
I don’t want to convert the model
to input
because i want to benefit from the two-way binding mechanism.