gpt4 book ai didi

嵌套表单数组的 Angular PatchValue

转载 作者:行者123 更新时间:2023-12-04 01:35:49 26 4
gpt4 key购买 nike

我有一个包含多个嵌套表单数组的响应式(Reactive)表单。在我的表单中,我可以有多个段和多个集合,问题是在从数据库获取数据时使用 patchValue() 或 setValue() 来编辑我的表单。我知道我需要创建多个表单组来修补值,是否有动态创建表单以匹配从数据库接收的数据的方法?

ngOnInit() {
this.myForm = this.fb.group({
id: '',
name: '',
userId: "",
description:'',
date: "",

segments: this.fb.array([
this.fb.group({
id:'',
workoutId:'',
name:'',
notes: '',
exerciseName:'',

sets: this.fb.array([
this.fb.group({
id:'',
segmentId:'',
name:'',
reps:'',
weight:'',
type:'',
success:'',
}),
])
})
])
});
this.workoutService.getWorkout(this.workoutId).subscribe(
(Workout) => { this.workout = Workout, this.setValueForm()},
(err) => console.log(err, "This did not load properlu make a toasty"),
)

setValueForm() {
this.myForm.patchValue(this.workout);

}

最佳答案

像这样尝试:

let segments = res.segments // value from db

const formArray = new FormArray([]);

segments.forEach(s => {
formArray.push(this.fb.group({
id: s.id,
name: s.name
}));
});


this.myForm.setControl('segments',formArray);

关于嵌套表单数组的 Angular PatchValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59588933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com