How to reset only specific fields of form in angular 5

前端 未结 5 2313
名媛妹妹
名媛妹妹 2021-02-19 20:45

I have created a function in one of my component file that resets the form(myform):

`onSubmit() {
  if (this.myform.valid) {
    console.log(\"Form Submitted!\")         


        
5条回答
  •  花落未央
    2021-02-19 21:26

    UPDATE:

    I just had this issue and although the accepted answer works, it has some tslint warnings. I ended up doing:

    this.myForm.get('formControlName').setValue(null);
    

    I'm working with Angular 8.

    And if you want to do it for several fields this works too:

    private controlNames = ['nameOne', 'nameTwo'];
    
    this.controlNames.map((value: string) => this.myForm.get(value).setValue(null));
    

提交回复
热议问题