如何在表单组中设置数组的值

问题描述

if (Object.prototype.hasOwnProperty.call(data,'checklists')) {
      if (Array.isArray(data.checklists)) {
        data.checklists.map((dt: any) => {
          dt.tasks.forEach((task: any) => {
            const dataArray = new FormGroup({});
            dataArray.addControl('failed',new FormControl(true,Validators.required));
            dataArray.addControl('remarks',new FormControl('',Validators.required));
            dataArray.addControl('task',new FormControl(task));
            formArray.push(dataArray);
          });
        });

        data.checklists.map((dt: any) => {
          assetArray.push(dt.asset);
        });
      }
    }
    const formField = {
      remarks: new FormControl(''),tasks: formArray,room: this.creds.credentials['room'],asset: assetArray
    };
    return this.fb.group(formField);

如何使用新的FormControl设置addControl的值,导致我要尝试的是使用create dataedit data创建表单。

我试图将其更改为:

            dataArray.addControl('failed',new FormControl(task['failed],new FormControl(task['remark'],new FormControl(task['task']));

,但不会显示表单中的数据。但是当它在控制台上检查时,它可以工作。

enter image description here

解决方法

您可以尝试使用patchValue来设置值吗?

这是我在这里写的一个小例子。

const group = new FormGroup({
  control1 : new FormControl(),control2 : new FormControl(),control3 : new FormControl()
});

const array = new FormArray([this.group]);

在这里,如果您需要将值设置为FormGroup意味着, 尝试使用patchValue()在组中设置FormControl的值

this.group.patchValue({control1:'your value here'})
this.array = new FormArray(this.group)

以下链接可能会为您提供帮助,

https://www.concretepage.com/angular-2/angular-2-formgroup-example

https://angular.io/guide/reactive-forms

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...