将数组的值组合到Reactive Forms中的FormArray行

时间:2018-03-19 04:39:45

标签: angular angular-reactive-forms angular-forms reactive-forms

我试图提交产品,我还需要this.totals[].amount and this.totals[].total.现在,我只能获得this.myForm.get('rows').value。我需要在行中插入this.totals []。amount和this.totals [] .total。我怎样才能做到这一点?这是我的代码链接

CLICK LINK HERE

onCreateProduct(){
   const formData = {
     products: this.myForm.get('rows').value
   }
   console.log(formData)

  }

patchValues() {
    let rows = this.myForm.get('rows') as FormArray;
    this.orders.forEach(material => {
      material.materials.forEach(x => {
        rows.push(this.fb.group({
          material_id: x.id,
          material_name: x.name,
          quantity: [null, Validators.required],
          price: [null, Validators.required],
          dis_checkbox: [true],
          checkbox: [1.12]
        })) //see that total and amount is NOT in the form
        this.totals.push({amount:0,total:0});  //<--add this line
      })
    })
    this.setOnChange();
  }

1 个答案:

答案 0 :(得分:1)

您可以在onCreateProduct功能中轻松地向行中添加数据。将代码更改为以下内容:

const formData = {
  products: this.myForm.get('rows').value.map((row, i) => {
    row.amount = this.totals[i].amount;
    row.total = this.totals[i].total;
    return row;
  })
}