I am creating my custom component, suppose api response is late(here I reproduced problem with setTimeOut()), then my ngModel is not updating until I select that component, I am using ChangedetectionStratagy OnPush
Here I am attaching stackblitz link directly . Please help
It is working fine when I am making ChangedetectionStratagy.Default
CodePudding user response:
Well since you are using onPush change detection strategy you have to trigger the change detection manually . with onPush the change detection is not trigger until component input changes. here is how you trigger it manually in your AppComponent
export class AppComponent implements OnInit {
constructor(private cf: ChangeDetectorRef) {} // insert changeDetectorRef
ngOnInit() {
setTimeout(() => {
this.selectedModel = 'CN';
this.cf.markForCheck() // line added
}, 2000);
}