表单数组填充到另一个组件

时间:2019-04-16 04:03:47

标签: javascript angular typescript

我正在使用reactive forms,并且内部具有表单数组。一个组件将值存储到数据库中,而我现在有了另一个组件来编辑这些值并更新它们。 但是我无法弄清楚表单数组,因为我无法将它们获取到formarray字段。它表示{}上不存在ForEach。有什么事吗 这是我的代码。

edit.component.ts

ngOnInit() {
    this.requestID = this.route.snapshot.params.requestID;
    this.formService.getFormDetails(this.formID).subscribe((data: any) => {
          data.forEach(customer => {
            if (customer) {
              this.customerFormEdit.patchValue({
                requestID: customer.requestID,
                requestType: customer.requestInformation.requestType,
                specialRequest: customer.specialRequest,
                sendTo_email: customer.requestInformation.sendTo.email
    // I know patch values is used to patch normal fields
    });
this.getEquip()
}
 getEquip() {
    const control = <FormArray>this.customerFormEdit.controls['equipment'];
    this.customers.equipment.forEach(x => {
      control.push(this.fb.group({ SerialNo: x.serialNo }))
    })
  }

这是我的客户模型的样子

export class CustomerForm {
  company: string;
  attention: string;
  email: string;
  requestType: string;
  specialRequest:string
  equipment: {
    serialNo: string;
    customerID: string;
  };
}

以及表单数组如何存储在JSON中

"installations" : [ 
        {
            "_id" : ObjectId("5cb54f48c39990305c8e026e"),
            "customerSerialNo" : "273823",
            "customerID" : "32324",
}]

// fb阵列

equipment:this.fb.array([this.fb.group({
serialNo:"",
customerID:""
})])

1 个答案:

答案 0 :(得分:0)

您已经在设备内部将设备声明为对象,而它应该是一个数组。

您的模型应与

分开
export class CustomerForm {
  company: string;
  attention: string;
  email: string;
  requestType: string;
  specialRequest:string
  equipment[];
}


export class equipment{
  serialNo: string;
  customerID: string;
}
相关问题