Pass value to array - angular

Asked

Viewed 167 times

0

I have this formGroup:

 this.formGroup = this.formBuilder.group({
  id: [null],
  code: [null, Validators.required],
  description: [null],
  percent: [null, Validators.required],
  dueDate: [null, Validators.required],
  dataCreate: [null],
  couponPlan: this.formBuilder.array([]),
});

}

and need to pass the values to couponPlan initially is just planId, that I need to get through. In the submit form, I’m trying something like:

this.formGroup.value['couponPlan'] =  this.formBuilder.group([this.plansids]);

However it does not work, it does not create the couponPlan array. I’ve tried so many ways this.formGroup.controls['couponPlan'] = this.formBuilder.group([this.plansids]);

It gets the values of planIds wrong. I need to fill in correctly, to receive these values from the list in my API. Look how planId is getting. inserir a descrição da imagem aqui

2 answers

0

this.formGroup.value['couponPlan'] =  this.formBuilder.array(this.plansids.map(plan=> new FormControl(plan)); // ou plan.propriedade
  • It didn’t work, it was the same as in the question.

  • I’m trying something like for (let i = 0; i < length; i++) {&#xA; const billInstallment = {}; &#xA; billInstallment['planId'] = new FormControl(this.selection.selected.map(x=> x.id));&#xA; form.push(new FormGroup(billInstallment));&#xA; } but it hasn’t worked out yet.

0


In formArray you need to include a list of abstractControls, as in the example below:

this.form = this.formBuilder.group({
    telefones: this.formBuilder.array([])
});

const telefones = ['(00) 0000 - 0000', '(11) 1111 - 1111'];
telefones.forEach(tel => (this.form.controls['telefones'] as FormArray).push(new FormControl(tel)));

If you need an object array you can use formGroup instead of formControl.

  • Thanks!! I managed to reach the solution, with the help of your comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.