I have a FormGroup that includes FormArray. When the user fulfills the form, he/she can dynamically add new controls. For this purpose I use FormArray. But after the form was submitted all the controls that the user added remained on the page. So I want to keep only one control in the FormArray after clicking the submit button.
Code is the following:
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 (don't judge it. No CSS yet :) )
You can see there are 2 Exercise fields now. I want it to be only 1 after I clicked submit. How can I do so?
