How to clear or reset form with Angular2

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 project

    
asked by anonymous 22.09.2016 / 15:32

1 answer

1

The fields on your form are linked to a template, right?

<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, redefine your model:

resetForm(){

    this.formulario = {
          nome:' '
    }

}

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

resetForm(){

  this.formulario = new Formulario();

}
    
22.09.2016 / 15:48