【问题标题】:Patch Value to FormArray Nested within FormArray of FormArray in Angular reactive Forms在 Angular 反应形式中嵌套在 FormArray 的 FormArray 中的 FormArray 的补丁值
【发布时间】:2021-09-16 18:00:55
【问题描述】:

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

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

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 值的数据的方法

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页面已经开发

【问题讨论】:

    标签: javascript typescript angular-reactive-forms formarray


    【解决方案1】:

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

    这个帖子可能会对你有所帮助。 Angular2 patchValue push value into array

    【讨论】:

      【解决方案2】:

      本教程帮助了我

      https://www.tektutorialshub.com/angular/setvalue-patchvalue-in-formarray-angular/
      

      我添加的示例上面的第二个 forEach 循环

      var p:FormGroup=this.newPointOfIntrest()
      

      然后按 p

      我添加的第三个 forEach 循环

      var m:FormGroup=this.newMeta()
      

      然后按m

      然后它对我有用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-07
        • 2018-01-29
        • 2021-07-07
        • 1970-01-01
        • 2021-06-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多