How to clear form after submit in Angular 2?

后端 未结 15 1545
遇见更好的自我
遇见更好的自我 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条回答
  •  猫巷女王i
    2020-12-02 16:44

    I found another solution. It's a bit hacky but its widely available in angular2 world.

    Since *ngIf directive removes the form and recreates it, one can simply add an *ngIf to the form and bind it to some sort of formSuccessfullySentvariable. => This will recreate the form and therefore reset the input control statuses.

    Of course, you have to clear the model variables also. I found it convenient to have a specific model class for my form fields. This way i can reset all fields as simple as creating a new instance of this model class. :)

提交回复
热议问题