简体   繁体   中英

How to use PatchValue inside a loop on a FormArray?

Hello everyone, I have a function where i pass the id when i use console.log(id) it's working fine but why i can't use patchValue inside a loop in a formArray? i want all the first 5 elements to have the same id.

for(let j=0; j<5; j++)
{
  this.myarray.at(j).patchValue([
    {
       elementid: id
       
     }
   ]);
  
}

I'm not sure that this is exactly what you were trying to do, but I did a small StackBlitz where I replace the values within a loop using splice . As you can see in the console, the first 5 elements of the array all have the same id after running the function, while the 'name' is unchanged.

There are probably better ways of achieving the same result though

this.myarray.controls[j].get('elementid').setValue(id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM