Angular FormArray值作为数组而不是对象

时间:2018-09-05 17:46:56

标签: angular

我想将“ trackings”的值转换为数组。现在,它会打印一个对象。见下文:

enter image description here

我需要 trackings 作为数组:trackings:['value','value']

在我当前的代码下面:

interface FormInterface {
  items: [{
    sku: string;
    quantity: string;
    received: string;
    trackings: Array < any > ;
  }];
  orderId: string;
  isPrime: boolean;
  reason: string;
}

addItemToForm() {
  const items = this.fb.group({
    sku: ['', Validators.required],
    quantity: ['', Validators.required],
    received: ['', Validators.required],
    trackings: this.fb.array([])
  });
  this.itemsForm.push(items);
}

addTrackingsToForm(control) {
  console.log(control);
  control.push(
    this.fb.group({
      trackings: [] as Array < any >
    })
  );
}

submitData() {
  console.log( < FormInterface > this.inputData.value);
  // this.returns.createReturn(this.inputData.value).subscribe(data => {
  //   this.returns.openSnackBar('Return Created!', '');
  //   this.inputData.reset();
  // });
}

谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您的addTrackingsToForm正在向FormGroup添加FormArray。它应该添加一个FormControl。在这种情况下,将其更改为control而不是array

addTrackingsToForm(control) {
    console.log(control);
    control.push(
      this.fb.control()
    );
  }