首页 > 解决方案 > Angular 2 稍后将表单控件转换为表单组

问题描述

我有一个表单数组,其中包含一个名为“foreground”的控件,该控件以 null 开头:

(<FormArray>this.myForm.get('rooms')).push(this.fBuilder.group({
                id: [element.id, [Validators.required]],
                background: null,
                foreground: null,
            }));

但是我怎样才能把它变成一个表单组,后面有字段呢?我试过这个:

room.controls.foreground(this.fBuilder.group({
                foregroundImageID: [null, [Validators.required]],
                foregroundImagePath: [null, [Validators.required]],
                foregroundXPosition: [null, [Validators.required]],
                foregroundYPosition: [null, [Validators.required]]
            }));

标签: angularangular-reactive-forms

解决方案


像这样构建你的表单

(<FormArray>this.myForm.get('rooms')).push(this.fBuilder.group({
                id: [element.id, [Validators.required]],
                background: null,
                foreground: this.fBuilder.array([]),
            }));

现在,前台作为数组工作,您可以将元素推入前台


推荐阅读