How to clean or reset form with Angular2

Asked

Viewed 1,654 times

1

How can I clear the fields of a form with Angular2? I need to reset the form

I’m using the final version of angular 2 in my design

1 answer

1

The fields of your form are linked to a template, certain?

<input type="text" [ngModel]="cadastro.nome" name="cadastro-nome />

Place a call to a method:

<button (click)="resetForm">Redefinir</button>

And in the method code, re-define your template:

resetForm(){

    this.formulario = {
          nome:' '
    }

}

If your form structure is in a class, you can do the following:

resetForm(){

  this.formulario = new Formulario();

}
  • Thanks!! Solved the second way, in the class structure

Browser other questions tagged

You are not signed in. Login or sign up in order to post.