Resetting a form in Angular 2 after submit

前端 未结 12 1956
悲哀的现实
悲哀的现实 2020-12-01 03:16

I am aware that Angular 2 currently lacks a way to easily reset a form to a pristine state. Poking around I have found a solution like the one below that resets the form f

12条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 03:57

    For Angular 2 Final, we now have a new API that cleanly resets the form:

    @Component({...})
    class App {
    
        form: FormGroup;
         ...
        reset() {
           this.form.reset();
       }
    }
    

    This API not only resets the form values, but also sets the form field states back to ng-pristine and ng-untouched.

提交回复
热议问题