How to Reset a Form in Angular

πŸ’‘ Concept Name

Resetting Forms – In Angular, you can reset forms to their initial or custom state using reset() on FormGroup (reactive) or NgForm (template-driven).

πŸ“˜ Quick Intro

Forms may need to be reset after a successful submission or upon user interaction. Angular provides a built-in reset() method to clear all values, states, and validation markers.

🧠 Analogy / Short Story

Resetting a form is like pressing β€œClear” on a calculator – it wipes out everything and gives you a clean slate for fresh input.

πŸ”§ Technical Explanation

  • 🧼 form.reset() clears all values and validation states.
  • βš™οΈ form.reset({ ... }) sets new initial values during reset.
  • πŸ§ͺ Useful in reactive and template-driven forms alike.
  • πŸ“¦ Resets touched, dirty, pristine, and validation status.
  • πŸ”„ Automatically disables fields that were initially disabled.

🎯 Purpose & Use Case

  • βœ… Clear the form after a successful submission.
  • βœ… Provide a reset button for user convenience.
  • βœ… Set a fresh state for the next form entry.

πŸ’» Real Code Example

// app.component.ts
import { Component } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

                    @Component({
                        selector: 'app-root',
  templateUrl: './app.component.html'
                        })
export class AppComponent {
  form = new FormGroup({
    name: new FormControl(''),
    email: new FormControl('')
  });

  resetForm() {
    this.form.reset(); // or this.form.reset({ name: '', email: '' });
  }
}
<form [formGroup]="form">
  <input formControlName="name" placeholder="Name">
  <input formControlName="email" placeholder="Email">
  <button type="button" (click)="resetForm()">Reset</button>
</form>

❓ Interview Q&A

Q1: How do you reset a reactive form?
A: Use the form.reset() method on a FormGroup instance.

Q2: Can you set values while resetting?
A: Yes, pass an object with values to reset().

Q3: What happens to touched/dirty states on reset?
A: They’re cleared (marked as untouched and pristine).

Q4: Does reset clear validation errors?
A: Yes, it resets the status as well.

Q5: How do you reset a template-driven form?
A: Use form.resetForm() on the NgForm reference.

πŸ“ MCQs

Q1. Which method resets a form in Angular?

  • form.clear()
  • form.reset()
  • form.delete()
  • form.empty()

Q2. Does reset clear validations?

  • No
  • Yes
  • Only for errors
  • Only warnings

Q3. How do you reset a template-driven form?

  • form.clearForm()
  • form.resetForm()
  • form.flush()
  • form.reInit()

Q4. What does reset() change?

  • Only values
  • Only errors
  • Values and state
  • Only touched flag

Q5. Can you pass values to reset()?

  • No
  • Yes
  • Only strings
  • Only nulls

Q6. What happens to pristine/dirty state after reset?

  • No effect
  • Reset to pristine
  • Becomes touched
  • Marked invalid

Q7. Is reset() available in FormControl?

  • No
  • Yes
  • Only FormGroup
  • Only NgModel

Q8. When would you typically use reset()?

  • After styling
  • When error occurs
  • After form submission
  • During rendering

Q9. Does reset disable controls?

  • Yes
  • No
  • Sometimes
  • Resets to initial state

Q10. What happens to disabled fields after reset?

  • They become enabled
  • They remain disabled
  • They are removed
  • They reset to null

πŸ’‘ Bonus Insight

Resetting is especially useful in multi-step forms where you want to clear user progress or start over. Always ensure your UI and business logic align with form resets.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

➑️ Next:

Learn More About Angular πŸš€

What is Angular vs AngularJS πŸ‘‰ Explained
Key Features of Angular πŸ‘‰ Explained
Angular Architecture πŸ‘‰ Explained
Components in Angular πŸ‘‰ Explained
Modules in Angular πŸ‘‰ Explained
Templates in Angular πŸ‘‰ Explained
Directives in Angular πŸ‘‰ Explained
Structural vs Attribute Directives πŸ‘‰ Explained
Services in Angular πŸ‘‰ Explained
Dependency Injection in Angular πŸ‘‰ Explained
Data Binding in Angular πŸ‘‰ Explained
Interpolation πŸ‘‰ Explained
Property Binding πŸ‘‰ Explained
Event Binding πŸ‘‰ Explained
Two-way Data Binding πŸ‘‰ Explained
ngModel Usage πŸ‘‰ Explained
Lifecycle Hooks πŸ‘‰ Explained
ngOnInit Hook πŸ‘‰ Explained
ngOnDestroy Hook πŸ‘‰ Explained
Pipes in Angular πŸ‘‰ Explained
Pure vs Impure Pipes πŸ‘‰ Explained
Create Custom Pipe πŸ‘‰ Explained
Angular Forms πŸ‘‰ Explained
Template-driven vs Reactive Forms πŸ‘‰ Explained
Form Controls πŸ‘‰ Explained
Form Validation πŸ‘‰ Explained
Validation Error Messages πŸ‘‰ Explained
FormGroup vs FormControl πŸ‘‰ Explained
Enable/Disable Form Fields πŸ‘‰ Explained
Reset Form πŸ‘‰ Explained
Routing in Angular πŸ‘‰ Explained
RouterModule πŸ‘‰ Explained
Configure Routes πŸ‘‰ Explained
Route Guards πŸ‘‰ Explained
CanActivate vs CanDeactivate πŸ‘‰ Explained
Lazy Loading πŸ‘‰ Explained
Preloading in Routing πŸ‘‰ Explained
Wildcard Routes πŸ‘‰ Explained
Route Parameters πŸ‘‰ Explained
Query Parameters πŸ‘‰ Explained
Programmatic Navigation πŸ‘‰ Explained
routerLink & routerLinkActive πŸ‘‰ Explained
Resolvers in Routing πŸ‘‰ Explained
Observables in Angular πŸ‘‰ Explained
RxJS in Angular πŸ‘‰ Explained
Observable vs Promise πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup