Tho*_*hom 10 angular angular-reactive-forms angular-forms
对于我正在构建的应用程序,我从 API 检索数据并直接使用该响应设置我的表单值。为此,我正在使用反应式表单模块。我已经构建了表单,使其与我从 API 获取的对象相匹配。问题是某些字段应该是一个对象 (FormGroup),但在为空时为 NULL。这会导致错误“无法将 undefined 或 null 转换为对象”。
表单组实例:
note: this.formBuilder.group({
turnbook_file: [''],
note: [''],
additional_files: ['']
});
Run Code Online (Sandbox Code Playgroud)
来自 API 的响应
note: NULL
Run Code Online (Sandbox Code Playgroud)
我的想法是,也许可以在定义 FormGroup 的地方放置 OR 语句或其他内容,如下所示:
note: this.formBuilder.group({
turnbook_file: [''],
note: [''],
additional_files: ['']
} | NULL);
Run Code Online (Sandbox Code Playgroud)
小智 1
您可以这样定义您的表单
note =new FormGroup({
'turnbook_file': new FormControl(null),
'note: new FormControl(null),
'additional_files': new FormControl(null)
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5868 次 |
| 最近记录: |