首页 > 解决方案 > Angular FormArray 值作为数组,而不是对象

问题描述

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

在此处输入图像描述

我需要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();
  // });
}

感谢您的帮助。

标签: angular

解决方案


addTrackingsToForm正在FormGroupFormArray. 它应该添加一个FormControl。在这种情况下将其更改controlarray

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

推荐阅读