How do you submit a reactive form and reset it afterwards?
Model answer
Bind (ngSubmit)="save()" on the form element and [formGroup]="form". In save(), check form.valid, read form.getRawValue() or form.value, and send it to a service. If it is invalid, call form.markAllAsTouched() so all errors become visible.
After a successful save, call form.reset() to clear it (pass default values if needed). Disable the submit button while the request is running to stop double submissions, and show a clear success or error message.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is the difference between template-driven and reactive forms?
- How do you add validation to a reactive form?
- How do you write a custom validator?
- What are async validators and when do you need one?
- What are FormArray and dynamic forms?
- What is the difference between valueChanges and statusChanges, and how do you use them safely?