0

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.

1
  • Your stackblitz was giving errors in console, I could not debug them... I created this: stackblitz.com/edit/… ... see if helps. Commented Sep 5, 2018 at 19:43

1 Answer 1

1

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()
    );
  }
Sign up to request clarification or add additional context in comments.

5 Comments

Hey. If I add control instead of group, then the tree changes: 'items -> 0 -> trackings -> 0 -> trackings' when it needs to be: items -> 0 -> trackings -> 0'
Would it be possible for you to create a Stackblitz?
I tried, but the template does not want to work. If you want to take a look at the code: stackblitz.com/edit/…
I've literally spent an hour building such a form a few days back. You might want to have a look at it here: stackblitz.com/edit/angular-complex-reactive-form
I will take a look at it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.