【问题标题】:ExpressionChangedAfterItHasBeenCheckedError: after modal rise and set property dataExpressionChangedAfterItHasBeenCheckedError:模态上升后设置属性数据
【发布时间】:2021-06-03 03:28:59
【问题描述】:

我有一个 ngModal 弹出窗口和一个带有 3 个控件的表单。其中一个控件是自动完成; 我试图在弹出窗口打开后填充数据。这里的重点是使用角度材质控制会导致异常发生。当我删除控件并打开弹出窗口时,一切都很顺利。 这是确切的错误

消息:'NG0100:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 'mat-focused' 的先前值:'false'。当前值:“真”

在单击按钮时从父级调用 ngModal 是这样的;

 const modalRef = this.modalService.open(CoinInsertModalComponent);
modalRef.componentInstance.coinAdded.subscribe((val: boolean) => {
  if (val) {
  //  this.getCoins();
  }
});

模态 HTML 是;

    <form class="pt-3" #f="ngForm" [formGroup]="coinForum" (ngSubmit)="addCoin()">
  <div class="form-group">
    <mat-form-field>
      <mat-label>Coin</mat-label>
      <input type="text" placeholder="Coin Seç" aria-label="Number" matInput formControlName="coinSelectControl"
        [matAutocomplete]="auto">
      <mat-autocomplete autoActiveFirstOption #auto="matAutocomplete">
        <mat-option *ngFor="let option of filteredOptions | async " [value]="option.pair">
          {{option.pair}}
        </mat-option>
      </mat-autocomplete>
    </mat-form-field>
  </div>
  <div class="form-group">

     <mat-form-field class="form-group">
      <mat-label>Alınan Mikdar</mat-label>
      <input formControlName="coinAmountControl" matInput placeholder="örn. 15.56" type="number">
    </mat-form-field>
  </div>
  <div class="form-group">
    <mat-form-field class="form-group">
      <mat-label>Alım Fiyatı</mat-label>
      <input formControlName="coinPriceControl" matInput placeholder="örn. 0.002346" type="number">
    </mat-form-field>
  </div>
  <div class="modal-footer">
    <button [disabled]="!f.valid" type="submit" class="btn btn-sm btn-success"><i
        class="fas fa-check"></i>Kaydet</button>
    <button class="btn btn-primary btn-sm" (click)="activeModal.dismiss('After Open')"><i
        class="fas fa-times"></i>İptal</button>

  </div>
</form>

最后的模态代码是;

 constructor(public activeModal: NgbActiveModal, private http: HttpClient, private spinner: NgxSpinnerService,
private toaster: ToastrService, private cd: ChangeDetectorRef) {
this.coinForum = new FormGroup({
  coinSelectControl: new FormControl('', [Validators.required]),
  coinAmountControl: new FormControl('', [Validators.required]),
  coinPriceControl: new FormControl('', [Validators.required])
});

}

 ngOnInit(): void {
const pairs = localStorage.getItem('pairs');
if (pairs) {
  if (new Date((JSON.parse(pairs)).getTime).getDate() !== (new Date()).getDate()) {
    this.getPairs();
  } else {
    this.options = JSON.parse(pairs).pairs;
  }
} else {
  this.getPairs();
}
this.filteredOptions = this.coinForum.controls.coinSelectControl.valueChanges.pipe(
  startWith(''),
  map(value => this._filter(value))
);

}

private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return this.options.filter((option: any) => option.pair.toLowerCase().indexOf(filterValue) === 0);

}

如果需要任何额外的信息来解决这个问题,请写在评论中,我会直接更新问题。 在此先感谢您的帮助

【问题讨论】:

    标签: angular-material bootstrap-modal angular11


    【解决方案1】:

    您需要手动让 Angular 知道在打开弹出模式时发生了一些变化。

    Angular 有一种机制可以听到这些变化。它是一个 ChangeDetectorRef 服务。

    使用 ChangeDetectorRef 服务来检测新的更改。在构造函数中注入如下:

    constructor (private cdRef: ChangeDetectorRef) {}
    

    通过this.cdRef.detectChanges() 检测更改,这应该可以为您解决问题。

    【讨论】:

    • 你不会在模态的哪个生命周期上注入它。我试过了,但没有成功!
    • 在 ngAfterViewInit() 生命周期钩子中试一试。
    • 好答案。在我的例子中,我在运行我的实际 Web 应用程序时没有注意到问题,但是我遇到了一个测试失败,ExpressionChangedAfterItHasBeenCheckedError 大约是 mat-focused。在ngAfterViewInit() 中调用detectChanges() 没有帮助,但在ngAfterContentChecked() 中调用detectChanges() 为我修复了它。不要忘记确保你的组件类实现了AfterContentChecked。
    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    相关资源
    最近更新 更多