【发布时间】: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