How to clear form after submit in Angular 2?

后端 未结 15 1399
遇见更好的自我
遇见更好的自我 2020-12-02 16:10

I have some simple angular 2 component with template. How to clear form and all fields after submit?. I can\'t reload page. After set data with date.setValue(\'\')

相关标签:
15条回答
  • 2020-12-02 17:00

    There is a new discussion about this (https://github.com/angular/angular/issues/4933). So far there is only some hacks that allows to clear the form, like recreating the whole form after submitting: https://embed.plnkr.co/kMPjjJ1TWuYGVNlnQXrU/

    0 讨论(0)
  • 2020-12-02 17:00

    this.myForm.reset();

    This is all enough...You can get the desired output

    0 讨论(0)
  • 2020-12-02 17:05

    Below code works for me in Angular 4

    import { FormBuilder, FormGroup, Validators } from '@angular/forms';
    export class RegisterComponent implements OnInit {
     registerForm: FormGroup; 
    
     constructor(private formBuilder: FormBuilder) { }   
    
     ngOnInit() {
        this.registerForm = this.formBuilder.group({
          empname: [''],
          empemail: ['']
        });
     }
    
     onRegister(){
        //sending data to server using http request
        this.registerForm.reset()
     }
    
    }
    
    0 讨论(0)
  • 2020-12-02 17:07

    Here is how I do it in Angular 7.3

    // you can put this method in a module and reuse it as needed
    resetForm(form: FormGroup) {
    
        form.reset();
    
        Object.keys(form.controls).forEach(key => {
          form.get(key).setErrors(null) ;
        });
    }
    

    There was no need to call form.clearValidators()

    0 讨论(0)
  • 2020-12-02 17:08

    See also https://angular.io/docs/ts/latest/guide/reactive-forms.html (section "reset the form flags")

    >=RC.6

    In RC.6 it should be supported to update the form model. Creating a new form group and assigning to myForm

    [formGroup]="myForm"
    

    will also be supported (https://github.com/angular/angular/pull/11051#issuecomment-243483654)

    >=RC.5

    form.reset();
    

    In the new forms module (>= RC.5) NgForm has a reset() method and also supports a forms reset event. https://github.com/angular/angular/blob/6fd5bc075d70879c487c0188f6cd5b148e72a4dd/modules/%40angular/forms/src/directives/ng_form.ts#L179

    <=RC.3

    This will work:

    onSubmit(value:any):void {
      //send some data to backend
      for(var name in form.controls) {
        (<Control>form.controls[name]).updateValue('');
        /*(<FormControl>form.controls[name]).updateValue('');*/ this should work in RC4 if `Control` is not working, working same in my case
        form.controls[name].setErrors(null);
      }
    }
    

    See also

    • https://github.com/angular/angular/issues/6196
    • https://github.com/angular/angular/issues/6169
    • https://github.com/angular/angular/issues/4933
    • https://github.com/angular/angular/issues/4914
    • https://github.com/angular/angular/issues/6371
    0 讨论(0)
  • 2020-12-02 17:08

    Make a Call clearForm(); in your .ts file

    Try like below example code snippet to clear your form data.

    clearForm() {
    
    this.addContactForm.reset({
          'first_name': '',
          'last_name': '',
          'mobile': '',
          'address': '',
          'city': '',
          'state': '',
          'country': '',
           'zip': ''
         });
    }
    
    0 讨论(0)
提交回复
热议问题