我正在尝试在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给出的错误与VSCode中的Type 'AbstractControl' is not assignable to type 'FormGroup'. Property 'controls' is missing in type 'AbstractControl'.类似。
请帮助我如何在angular-6中使用嵌套的form-group
发布于 2019-01-20 03:18:13
您可以尝试以下代码:
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': []
});发布于 2019-01-19 23:26:38
如果你检查任何嵌套(或父) FormGroup的类型,它将返回always object。所以你能做的就是将你的object类型的变量转换成这样-
apiForm : Object;
...
this.apiForm = this.serviceItemForm.get('api');否则,您可以在为变量赋值时进行类型转换,如下所示-
apiForm : FormGroup;
this.apiForm = (<FormGroup>this.serviceItemForm.get['api']);https://stackoverflow.com/questions/54268563
复制相似问题