这里我创建了动态表单来更新表单值
我在将值修补到元数组的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: [''],
})
这是我们将值修补到名称和城市窗体Controll 的代码
这里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页面已经开发
在打补丁之前,您必须找到应该向formArray添加多少表单控件。
此线程可能会对您有所帮助。Angular2补丁值将值推入阵列
本教程帮助了我
https://www.tektutorialshub.com/angular/setvalue-patchvalue-in-formarray-angular/
第二个对于上面的每个循环,我添加了
var p:FormGroup=this.newPointOfIntrest()
然后按下p
第三个循环我添加了
var m:FormGroup=this.newMeta()
然后推送m
然后它对我起了作用