angular - Angular6中的嵌套表单组
问题描述
我正在尝试在 Angular6 中创建嵌套表单组。
serviceItemForm: FormGroup;
apiForm : FormGroup;
this.serviceItemForm = this.fb.group({
'name': new FormControl('', Validators.required),
'description': new FormControl('', Validators.required),
'categoryIds': new FormControl([], Validators.required),
'tags': [],
'status': '',
'orderFormType': new FormControl([], Validators.required),
'orderFormKey': new FormControl([], Validators.required),
'workflow': [],
'orderFormAction': new FormControl('', Validators.required),
'customUI': new FormControl(''),
'api': this.fb.group({
'apiurl': new FormControl(''),
'apimethod': new FormControl(''),
'headers': this.fb.array([]),
'payload': '',
}),
'userGroup': []
});
this.apiForm = this.serviceItemForm.get('api');
这里 this.apiForm 给出了像Type 'AbstractControl' is not assignable to type 'FormGroup'. Property 'controls' is missing in type 'AbstractControl'.
在 VSCode 中一样的错误。
请帮助我如何在 angular-6 中使用嵌套的表单组
解决方案
你可以试试下面的代码:
serviceItemForm: FormGroup;
apiForm : FormGroup;
this.apiForm = this.fb.group({
'apiurl': new FormControl(''),
'apimethod': new FormControl(''),
'headers': this.fb.array([]),
'payload': '',
});
this.serviceItemForm = this.fb.group({
'name': new FormControl('', Validators.required),
'description': new FormControl('', Validators.required),
'categoryIds': new FormControl([], Validators.required),
'tags': [],
'status': '',
'orderFormType': new FormControl([], Validators.required),
'orderFormKey': new FormControl([], Validators.required),
'workflow': [],
'orderFormAction': new FormControl('', Validators.required),
'customUI': new FormControl(''),
'api': this.apiForm,
'userGroup': []
});
推荐阅读
- pointers - `if` 检查 nil 指针结构链中的属性的优雅方法
- excel - 在折线图中,如何将活动图表中的最后一个系列更改为辅助轴?
- vba - 宏没有在文本中拾取连字符
- android - FlatList在水平模式(RTL)下数据太大时自动滚动到第一
- flutter - StatefulWidget项的Listview
- c# - 是否可以在 azure app 服务中安装 c++ 编译器
- web - 后端和前端开发人员之间的信息共享
- node.js - 我想让成员在不和谐机器人中计数,但它给出了错误
- javascript - 将参数传递给函数
- javascript - Jquery根据输入检查值隐藏div