How to clear form after submit in Angular 2?

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

    To reset your form after submitting, you can just simply invoke this.form.reset(). By calling reset() it will:

    1. Mark the control and child controls as pristine.
    2. Mark the control and child controls as untouched.
    3. Set the value of control and child controls to custom value or null.
    4. Update value/validity/errors of affected parties.

    Please find this pull request for a detailed answer. FYI, this PR has already been merged to 2.0.0.

    Hopefully this can be helpful and let me know if you have any other questions in regards to Angular2 Forms.

提交回复
热议问题