I have two components; parent and child, which communicates via @Input
and @Output
property decorators. i.e. Parent passes an input object named config
to the child and listens to the child's emitted events.
where config object type is,
config:any = {
a: number;
b: number;
}
here to keep things simple config.a
is managed by child so parent never changes it and similarly config.b
is managed by the parent.
I observed following strange behaviors while setting up this communication model.
- Parent passes the required input via property binding i.e.
[config]="config"
normally property binding works only one way, but here its behaving as two way binding. i.e. when ever parent updates config.b
in its own scope, child's config also gets updated and vice versa.
child wants to be notified of any change which parent makes to its own config object, so it implements the
OnChanges
life cycle hook, to perform some inner functionality. But because an object is being changed which are immutable in nature, so child won't be notified of the changes unless,it updates config object reference like,
this.config = new Config()
or uses
onPush
change detection strategy and calls the following function to notify the child about the change.changeDetectorRef.markForCheck()
but when I try changeDetectorRef.markForCheck()
child's ngOnChanges
life cycle hook is never triggered.
Can anyone explain the reason behind both of these strange issues. I have replicated both issues in this plunker for reference.