我正在嘗試檢查物件是否具有值并且不會引發錯誤。如果是這樣,請將其作為輸入欄位的值傳遞。
export class DialogServiceTabletAddRowComponent implements OnInit {
service?: string;
constructor(private _dialogRef: MatDialogRef<DialogServiceTabletAddRowComponent>, private _fb: FormBuilder, @Inject(MAT_DIALOG_DATA) public data: ServiceDataToDialog) {
try {
this.service = this.data[this.data.id].allServices[this.data.id][this.data.id].service;
}
catch (e) {
this.service = '';
}
console.log(this.service);
})
}
mainForm = this._fb.group({
service: [this.service, Validators.required]
})
}
console.log(this.service);給了我正確的值,但我的輸入欄位的值仍然是空的。怎么會?
uj5u.com熱心網友回復:
this.mainForm = this._fb.group...在 try/catch 之后將代碼放入建構式中。也添加public mainForm: FormGroup為類屬性
這里的問題是 TypeScript 如何處理初始化類屬性。在引擎蓋下,它立即在建構式中初始化它們
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/482454.html
