Sole
Sole

Reputation: 3340

Reset formControl field on submit in Angular

I have a form which onSubmit should reset the value of specific form field. So far I have tried the following:

Ts file:

ngOnInit() {
    this.loading = true;
    this.getIncident();
    this.loading = true;
      })
      this.addCommentsForm = new FormGroup({
        comment: new FormControl(''),
        number: new FormControl('')
      })
    }

onSubmit() {

      let putData = Object.assign({}, this.addCommentsForm.value);
      console.log('form, ', putData)
      this.uploading = true;
      this.commentProgress.progress = 100;
      this.service.putIncidentsComments(putData, this.s_id).subscribe((response: any) => {
        console.log("comment sent");
        this.getIncidentComments();
        this.uploading = false;
        this.commentProgress.progress = 0;
        this.addCommentsForm.reset();
      }, (errorResponse: any) => {
        console.log(errorResponse); //On unsuccessful response
        this.error = true;
        this.uploading = false;
      });
    }
  }

the line: this.addCommentsForm.reset(); resets the whole form and I just want the comment field reset? any ideas?

Upvotes: 0

Views: 30

Answers (1)

Sachin Gupta
Sachin Gupta

Reputation: 5311

You can get the formControl and reset it:

this.addCommentsForm.get('comments').reset();

Upvotes: 1

Related Questions