【发布时间】:2022-01-25 16:09:13
【问题描述】:
我有一个包含 FormArray 的 FormGroup。 当用户完成表单时,他/她可以动态添加新控件。为此,我使用 FormArray。 但是在提交表单后,用户添加的所有控件都保留在页面上。 所以我想在点击提交按钮后在FormArray中只保留一个控件。
代码如下:
workoutForm = this.fb.group({
name: ['', Validators.required],
completed: [false],
exercises: this.fb.array([this.createExercise()])
})
createExercise(): FormGroup {
return this.fb.group({
name: ['', Validators.required],
completed: [false]
})
}
onSubmit() {
// Here I want to delete all elements from the exercises and to keep only one
this.workoutForm.reset();
}
UI (不要评判它。还没有 CSS :))
您现在可以看到有 2 个练习字段。单击提交后,我希望它仅为 1。我该怎么做?