我有一个表单,用户可以在其中填写他们的锻炼。我想调用addSet()
并将一个FormGroup推送到sets
FormArray
ngOnInit() {
this.workoutForm = new FormArray([
new FormGroup({
name: new FormControl(),
sets: new FormArray([
new FormGroup({
reps: new FormControl(),
weight: new FormControl(),
}),
]),
})
]);
}
public addSet() {
this.workoutForm.controls[0].controls.sets.push(
new FormGroup({
reps: new FormControl(),
weight: new FormControl(),
}),
)
}
1条答案
按热度按时间iq0todco1#
从Angular 文档复制粘贴(根据示例重命名变量):
https://angular.io/guide/reactive-forms#creating-dynamic-forms