Array objects in angular

Asked

Viewed 37 times

-2

I’m doing it this way to fill the array:

let form1 = this.formGroup.get('array_activity') as FormArray;
    console.log(form1);
    if (!this.data.isEdit) {
    for (let i = 0; i < form1.controls.length; i++) {  
      this.activity.name = form1.at(i).get('name').value;
      this.activity.qtd = form1.at(i).get('qtd').value;
      this.activity.unit = form1.at(i).get('unit').value;
      
      this.phase.activities.push(this.activity);
      console.log(this.activity);
   }

   console.log(this.phase.activities);

But in this way, for example, if I add 1 and 2, it adds 2 and 2, if it adds 1.2 and 3, it gets 3.3 and 3. He picks up the amount and it looks like he’s only adding the last one. When I give a console.log(this.activity); within the for, the values are correct. Any idea, and explanation of what might be ? Thank you.

1 answer

-2

I managed to settle by doing this way:

 for (let i = 0; i < form1.controls.length; i++) {  
  this.phase.activities.push({
  name:form1.at(i).get('name').value,
  qtd: form1.at(i).get('qtd').value,
  unit: form1.at(i).get('unit').value,
});
}

Browser other questions tagged

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