“Expression has changed after it was checked” Angular popup error

前端 未结 1 1435
梦谈多话
梦谈多话 2020-12-10 18:58

I have seen couple of articles about this error, I have went through some but couldn\'t find the solution. Here i am calling alert method when my Boolean is true. The alert

相关标签:
1条回答
  • 2020-12-10 19:23

    Use ChangeDetectorRef Service to force change Detection

    When a view uses the OnPush (checkOnce) change detection strategy, explicitly marks the view as changed so that it can be checked again.

    import { Component, Input, ChangeDetectionStrategy,ChangeDetectorRef } from '@angular/core';
    
    @Component({
      selector: 'component',
      templateUrl: 'component.html',
      changeDetection: ChangeDetectionStrategy.OnPush
    })
    
    
       constructor(cdr:ChangeDetectorRef){} 
       ngAfterViewInit() {
                let controlBlurs: Observable<any>[] = this.formControls
                    .map((formControl: ElementRef) => Observable.fromEvent(formControl.nativeElement, 'blur'));
        // debounceTime(1000)/
                Observable.merge(this.orderUnitForm.valueChanges, ...controlBlurs).subscribe(value => {
                    this.displayMessage = this.genericValidator.processMessages(this.orderUnitForm);
                   // this.valid = this.genericValidator.validateControls(this.orderUnitForm);
                });
                this.orderUnitForm.valueChanges.debounceTime(1000).subscribe(value => {
                    this.valid = this.genericValidator.validateControls(this.orderUnitForm);
                    this.commaSeparation = this.genericValidator.validateMultiComma(this.orderUnitForm);
                      if(this.commaSeparation == true){
                        this.displayModel();
                      }
                });
         this.cdr.detectChanges();
          }
    
    0 讨论(0)
提交回复
热议问题