[英]Angular 2 Turn form control into form group later
I have a form array with a control called "foreground" which is null to start with:我有一个表单数组,其中包含一个名为“foreground”的控件,它的开头为 null:
(<FormArray>this.myForm.get('rooms')).push(this.fBuilder.group({
id: [element.id, [Validators.required]],
background: null,
foreground: null,
}));
But how can I turn it onto a form group with fields under it later on?但是,我以后如何将其转换为带有字段的表单组? I tried this:
我试过这个:
room.controls.foreground(this.fBuilder.group({
foregroundImageID: [null, [Validators.required]],
foregroundImagePath: [null, [Validators.required]],
foregroundXPosition: [null, [Validators.required]],
foregroundYPosition: [null, [Validators.required]]
}));
Build your form like this像这样构建你的表单
(<FormArray>this.myForm.get('rooms')).push(this.fBuilder.group({
id: [element.id, [Validators.required]],
background: null,
foreground: this.fBuilder.array([]),
}));
Here Now foreground working as Array, With that you can push elememts into foreground现在前台作为数组工作,这样您就可以将元素推入前台
You can do it something like below:您可以执行以下操作:
(<FormArray>this.myForm.get('rooms')).push(this.fBuilder.group({
id: [element.id, [Validators.required]],
background: null,
foreground: null
}));
Now SetControl
like this-现在像这样
SetControl
this.myForm.controls.rooms['controls'][0].setControl('foreground', this.fBuilder.group({
foregroundImageID: [null, [Validators.required]],
foregroundImagePath: [null, [Validators.required]],
foregroundXPosition: [null, [Validators.required]],
foregroundYPosition: [null, [Validators.required]]
}));
Note: setControl
replaces the existing control.注意:
setControl
替换现有控件。 learn more about setControl了解有关 setControl 的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.