我正在尝试检查对象是否具有值并且不会引发错误。如果是这样,请将其作为输入字段的值传递。
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);
给了我正确的值,但我的输入字段的值仍然是空的。怎么会?
this.mainForm = this._fb.group...
在 try/catch 之后将代码放入构造函数中。也添加public mainForm: FormGroup
为类属性
这里的问题是 TypeScript 如何处理初始化类属性。在引擎盖下,它立即在构造函数中初始化它们
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句