简体   繁体   English

Angular 2 稍后将表单控件转换为表单组

[英]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.

相关问题 Angular 表单构建器 vs 表单控件和表单组 - Angular form builder vs form control and form group 响应式表单中的 Angular 表单组与表单控件和表单构建器 - Angular form group vs form control and form builder in Reactive Forms 如何以角度反应形式访问表单控件和表单组 - How to access form control and form group in angular reactive form 如何在 angular 中动态添加表单控制组 - how to add form control group dynamically in angular Angular 反应式表单将表单组转换为空表单控件 - Angular reactive forms turn formgroup into null form control Angular 从表单组按名称访问表单控件 - Angular access form control by name from form group 表单组的表单数组,获取精确特定控件的更改 - angular - Form array of form group , get changes for exact specific control - angular 如何在html中的表单组上关闭angular 8 Forms中的自动完成? - How to turn off the auto complete in angular 8 Forms on form group in html? Angular Material动态按钮切换组表单控件访问器 - Angular Material dynamic button toggle group form control accessor Angular form group:找不到具有未指定name属性的控件 - Angular form group: cannot find control with unspecified name attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM