Home > other >  ngmodel not working in child component for on push change detection
ngmodel not working in child component for on push change detection

Time:11-03

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

https://stackblitz.com/edit/primeng-dropdown-demo-2t9ed3?file=src/single-select/single-select.component.ts

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);
  }

https://stackblitz.com/edit/primeng-dropdown-demo-jbzchf?file=src/single-select/single-select.component.html

  • Related