Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何在FormArray上的循环内使用PatchValue?_Angular_Formarray - Fatal编程技术网

Angular 如何在FormArray上的循环内使用PatchValue?

Angular 如何在FormArray上的循环内使用PatchValue?,angular,formarray,Angular,Formarray,大家好,我有一个函数,当我使用console.log(id)时,我在其中传递id。它工作得很好,但是为什么我不能在formArray的循环中使用patchValue呢?我希望前5个元素具有相同的id for(let j=0; j<5; j++) { this.myarray.at(j).patchValue([ { elementid: id } ]); } for(让j=0;j我不确定这是否正是您想要做的,但我做了一个小测

大家好,我有一个函数,当我使用console.log(id)时,我在其中传递id。它工作得很好,但是为什么我不能在formArray的循环中使用patchValue呢?我希望前5个元素具有相同的id

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

for(让j=0;j我不确定这是否正是您想要做的,但我做了一个小测试,使用
splice
替换循环中的值。 正如您在控制台中看到的,在运行函数后,数组的前5个元素都具有相同的id,而“name”没有改变


也许有更好的方法可以达到同样的效果,但是你得到了哪个错误?我没有得到任何错误…但是elementId没有收到id…如果我使用相同的patchValue,而不是在循环中,并且只对它工作的第一个元素,你确定你的“at”函数吗?Stackblitz在数组中不知道它。我看了它启动了,似乎是一个“实验函数”:经过其他一些研究,我发现了这个.myarray.controls[j].get('elementid').setValue(id);它和你的解决方案一样工作得很好谢谢你花了这么多时间,我真的很感激。