19

I have tried and failed to find the way in which to reset my angular form.

Can somebody help?

<form #thisIsAForm>
  <mat-form-field class="full-width">
    <input matInput placeholder="Weather">
  </mat-form-field>
</form>
<button mat-raised-button (click)="resetForm()">Reset</button>

export class Example{
  @ViewChild('thisIsAForm') thisIsAForm;

  resetForm() {
    this.thisIsAForm.reset();
  }
}

6 Answers 6

27

Almost ! Use a reactive form for that :

<form [formGroup]="myForm">
  <mat-form-field class="full-width">
    <input matInput placeholder="Weather" formControlName="weather">
  </mat-form-field>
</form>
<button mat-raised-button (click)="myForm.reset()">Reset</button>


export class Example{
  myForm: FormGroup;

  constructor(private fb: FormBuilder) { 
    this.myForm = fb.group({
      weather: ''
    });
  }

  // If the HTML code doesn't work, simply call this function
  reset() {
    this.myForm.reset();
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This works perfectly in the stackblitz, however when I am using it within my actual code I am getting the dreaded error Can't bind to 'formGroup' since it isn't a known property of 'form'. I have checked everything and found that I have imported both FormsModule and ReactiveFormsModule properly... I don't get what is going wrong.
SCRATCH THAT! I found it was me being an idiot. I'd forgotten that the components using the clear form feature are themselves imported into app.module.ts via another import file, so I added ReactiveFormsModule as an import to the intermediary file and all is good!
4
<form [formGroup]="thisIsAForm" (ngSubmit)="onSubmit()">
  <mat-form-field class="full-width">
    <input formControlName="weather" placeholder="Weather">
  </mat-form-field>
</form>
<button mat-raised-button (click)="resetForm()">Reset</button>


export class Example{
  thisIsAForm: FormGroup;

  constructor() {
    this.thisIsAForm = new FormGroup(
      weather: new FormControl('')
    ); 
  }

  resetForm() {
    this.thisIsAForm.reset();
  }
}

1 Comment

Arg @trichetriche was faster :P
1

In Angular 8, if you do

<form #form="ngForm" (ngSubmit)="process(form)">

process(form: NgForm) { ...

You'll get the following error when you build with --prod

Argument of type 'FormGroupDirective' is not assignable to parameter of type 'NgForm'.

on (ngSubmit)

What I did was to inject form template reference as FormGroupDirective then call resetForm().

<form #form (ngSubmit)="process(form)">

@ViewChild('form', { static: false }) FormGroupDirective formDirective;

See FormGroupDirective

Comments

1

I think you need to add ngForm directive in the html code as mentioned below stackbliz example also add name and ngModel ngControl to form elements.

Stackbliz: Template Driven Form Reset

Comments

1

For me adding type="reset" to the reset button resolved issue

<button **type = "reset"** mat-raised-button (click)="resetForm()">Reset</button>

Comments

0

Here I m resetting the template-driven form without making form dirty.

    <form class="example-form" #charreplaceform="ngForm">
          <mat-form-field class="example-full-width">
            <input matInput #codepointSel="ngModel" (change)="createReplacementChar()" (keyup)="checkForNumber()"
              required [(ngModel)]="charReplaceInfo.codePoint" name="code-point" placeholder="Code Point (Only number)"
              [disabled]="isDisabled">
          </mat-form-field>

          <a (click)="refreshCharRecord(charreplaceform)">
            <i class="material-icons">
              loop
            </i>
          </a>
    </form>

    // in .ts
    refreshCharRecord(form: NgForm) { // getting the form reference from template 
    form.form.reset();   // here we are resetting the form without making form dirty
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.