在这里,我创建了动态表单来更新表单值
我在将值修补到元数组的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页面已经开发了
发布于 2021-07-08 09:14:21
这个教程帮了我
https://www.tektutorialshub.com/angular/setvalue-patchvalue-in-formarray-angular/上面的例子中的第二个forEach循环
var p:FormGroup=this.newPointOfIntrest()然后推p
我添加了第三个forEach循环
var m:FormGroup=this.newMeta()然后推m
然后我起了作用
发布于 2021-07-06 05:14:38
在修补之前,您必须找到应该向formArray添加多少个表单控件。
这条线可能对你有帮助。Angular2 patchValue push value into array
https://stackoverflow.com/questions/68264872
复制相似问题