HOWTO · Angular

How to Reset Forms in Angular 2

Learn how to reset forms in Angular 2 effectively with our comprehensive guide. Explore various methods including using resetForm(), resetting to initial values, and managing individual form controls. Enhance your application's user experience by mastering these essential techniques for form management.

On this page

Resetting a form in Angular means restoring its controls to a clean state: values go back to null or to defaults you supply, and state flags such as dirty, touched, and submitted are cleared. The call you use depends on which of Angular’s two form APIs your form is built with, so the first step is identifying that.

The snippets below are Angular application source; they run inside an Angular project rather than as standalone scripts, so the templates are shown as browser markup and the class-side calls as inline code without claimed console output. A small standalone script at the end models the resulting state so the behavior can be checked directly.

Choose the reset API for your Angular form

Angular 2 ships two form modules, and each has its own reset method:

  • Template-driven forms come from FormsModule. Angular automatically attaches the ngForm directive to every <form> element, creating an NgForm object that tracks each control’s value, validity, and state. You reset the whole form through that object with resetForm().
  • Reactive forms come from ReactiveFormsModule. You build a FormGroup in the component class and bind it in the template with [formGroup]. You reset it with the group’s own reset() method.

These are different APIs: resetForm() exists on NgForm, while reset() exists on FormGroup and on individual controls. Check which module your application imports and pick the matching call.

Reset a template-driven form with NgForm

With FormsModule imported in your NgModule, capture the NgForm instance in the template by assigning #myForm="ngForm". The component’s template file then reads:

<form #myForm="ngForm" (ngSubmit)="onSubmit(myForm)">
  <input name="username" ngModel required>
  <input name="email" ngModel required>
  <button type="submit">Submit</button>
  <button type="button" (click)="myForm.resetForm()">Reset</button>
</form>

Clicking Reset invokes resetForm() on the NgForm instance. Every control’s value returns to null, and Angular clears the dirty, touched, and submitted flags so the form behaves as if the user had never interacted with it. To keep the call inside the class instead, bind the button to (click)="onReset(myForm)", import NgForm from @angular/forms, and define onReset(form: NgForm) { form.resetForm(); } in the component.

Reset a reactive form with FormGroup

A reactive form keeps its model in the component class. With ReactiveFormsModule imported, create the group in the class — for example loginForm = new FormGroup({ username: new FormControl(''), email: new FormControl('') }) — and bind it in the template with [formGroup]:

<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
  <input formControlName="username">
  <input formControlName="email">
  <button type="submit">Submit</button>
  <button type="button" (click)="loginForm.reset()">Reset</button>
</form>

Calling reset() on the FormGroup sets every control’s value to null and restores the pristine and untouched state flags; the same call works inside the class as this.loginForm.reset(). It is the reactive counterpart of NgForm.resetForm(), not the same method.

Restore default values or reset one control

Both reset methods accept an optional value object, which is the way to restore defaults instead of clearing the fields. Pass an object whose keys match your control names — form.resetForm({ username: 'guest', email: 'guest@example.com' }) for a template-driven form, or this.loginForm.reset({ username: 'guest', email: 'guest@example.com' }) for a reactive one.

Keep the keys aligned with the control names you registered. Unlike patchValue(), reset() is meant to describe the whole group’s state, so matching names is what keeps every control landing on the value you expect.

To reset only one field while leaving the others as the user left them, call reset() on that single control: form.controls['username'].reset() through the NgForm in a template-driven form, or this.loginForm.get('username').reset() through the FormGroup in a reactive form. The individual reset() clears that control’s value and its own dirty and touched flags; sibling controls keep their values and state.

Check the values and state after a reset

After a whole-form reset, each control’s value is null (or the default you passed), form.pristine and form.untouched are true again, and dirty and touched return to false. For a template-driven form, NgForm also clears its submitted flag, so error messages guarded by something like myForm.submitted && username.invalid disappear until the next submit.

Resetting does not remove validators. A control marked required stops being valid as soon as its value resets to null, so form.valid reflects that immediately even though the field looks untouched. If you only want to clear the interaction flags while keeping the entered values, call markAsPristine() and markAsUntouched() on the form or control instead of resetting.

The value and flag transitions are small enough to verify outside an Angular project. Run this example:

function resetControl(control, value = null) {
  control.value = value;
  control.dirty = false;
  control.touched = false;
}

const username = { value: 'adil', dirty: true, touched: true };
const email = { value: 'adil@example.com', dirty: true, touched: true };

resetControl(username);
resetControl(email, 'guest@example.com');

console.log('username:', username);
console.log('email:', email);

Output:

username: { value: null, dirty: false, touched: false }
email: { value: 'guest@example.com', dirty: false, touched: false }

The first call mirrors control.reset() with no argument — the value drops to null and both flags clear — while the second mirrors control.reset('guest@example.com'), restoring a default value with the same flag cleanup.