首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以角反应形式嵌套于FormArray的FormArray of FormArray中的补丁值

以角反应形式嵌套于FormArray的FormArray of FormArray中的补丁值
EN

Stack Overflow用户
提问于 2021-07-06 05:09:26
回答 2查看 473关注 0票数 0

在这里,我创建了动态表单来更新表单值

我在将值修补到元数组的formController时遇到了问题

代码语言:javascript
复制
profile:FormGroup;

constructor(public http: HttpClient, private fb: FormBuilder){

    this.profile=this.fb.group({

name: ['', Validators.required],

sections: this.fb.array([this.createDestinationSection()])

 })

}
createDestinationSection() {

return this.fb.group({

 city: [''],

pointsOfInterests: this.fb.array([this.createPointsOfInterests()])

})

}

createPointsOfInterests(){

return this.fb.group({

 meta: this.fb.array([this.createPointOfInterestMeta()])

})
createPointOfInterestMeta(){

return this.fb.group({

type: [''],
})

这是我们将值修补为name和city formControll的代码

这里,find()是我们从数据库中获取特定id值数据的方法。

代码语言:javascript
复制
this.find(this.id)
      .subscribe((data) => {

     const destinationSection = this.profile.get('sections') as FormArray;

     destinationSection.clear();
     
     data.sections.forEach((item) => {

      const pointsofInterests = item.pointsOfInterests.map((d) => new FormGroup({

      city: new FormControl(d.city),

      meta:this.fb.array([]),

     }))
  
    destinationSection.push(this.fb.group({

     name: [item.name, Validators.required],

     pointsOfInterests: this.fb.array(pointsofInterests),

     }))

    })
    
 })

在如何将值修补到元数组方面,我有一个问题;

注释: html页面已经开发了

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-08 09:14:21

这个教程帮了我

代码语言:javascript
复制
https://www.tektutorialshub.com/angular/setvalue-patchvalue-in-formarray-angular/

上面的例子中的第二个forEach循环

代码语言:javascript
复制
var p:FormGroup=this.newPointOfIntrest()

然后推p

我添加了第三个forEach循环

代码语言:javascript
复制
var m:FormGroup=this.newMeta()

然后推m

然后我起了作用

票数 0
EN

Stack Overflow用户

发布于 2021-07-06 05:14:38

在修补之前,您必须找到应该向formArray添加多少个表单控件。

这条线可能对你有帮助。Angular2 patchValue push value into array

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68264872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档