How to clear form after submit in Angular 2?

后端 未结 15 1398
遇见更好的自我
遇见更好的自我 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:09

    Here's how I do it Angular 8:

    Get a local reference of your form:

    <form name="myForm" #myForm="ngForm"></form>
    
    @ViewChild('myForm', {static: false}) myForm: NgForm;
    

    And whenever you need to reset the form, call resetForm method:

    this.myForm.resetForm();
    

    You will need FormsModule from @angular/forms for it to work. Be sure to add it to your module imports.

    0 讨论(0)
  • 2020-12-02 17:09
    resetForm(){
        ObjectName = {};
    }
    
    0 讨论(0)
  • 2020-12-02 17:10

    As of Angular2 RC5, myFormGroup.reset() seems to do the trick.

    0 讨论(0)
提交回复
热议问题