Blinhawk
Blinhawk

Reputation: 389

Angular FormArray value as an array, not object

I want to convert the values of "trackings" to an array. Right now, it prints an object. See below:

enter image description here

I need trackings to be an array: trackings: ['value','value']

Below my current code:

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

Thank you for the help.

Upvotes: 0

Views: 1234

Answers (1)

SiddAjmera
SiddAjmera

Reputation: 39482

Your addTrackingsToForm is adding a FormGroup to the FormArray. It should be adding a FormControl instead. Change it to control instead of array in that case:

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

Upvotes: 1

Related Questions