【发布时间】: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