Meenal
Meenal

Reputation: 145

Form is not getting refreshed after I do (ngSubmit) in angular 6

Below is snippet of app.component.html

<form *ngIf="addForm" [formGroup]="addForm" (ngSubmit)="onSubmit()">
  <table>
    <thead></thead>
    <tbody>
      <tr>
        <td>

In app.component.ts

onSubmit() {
  console.log(this.addForm.value);
  this.contractService.saveContract(this.addForm.value)
    .subscribe(data => {
      alert('Contract created successfully');
    });
}

My problem is after I get the alert, the form values should automatically get cleared i.e. the form should be refreshed. It's not happening for me. Any suggestion is appreciable.

Upvotes: 2

Views: 6764

Answers (2)

Aniket Avhad
Aniket Avhad

Reputation: 4145

You have to do clear form after alert ok click

Here's an example

onSubmit() {
  console.log(this.addForm.value);
  this.contractService.saveContract(this.addForm.value)
    .subscribe(data => {
       if (!alert("Contract created successfully")) {
          this.addForm.reset();
       }

    });
}

You can also use window.confirm

onSubmit() {
      this.contractService.saveContract(this.addForm.value)
        .subscribe(data => {
           if ( window.confirm('Contract created successfully') ) {
               //clicked Yes
               this.addForm.reset();
            }
        });
    }

Upvotes: 1

FAISAL
FAISAL

Reputation: 34691

You need to call reset() method of the form.

onSubmit() {
  console.log(this.addForm.value);
  this.contractService.saveContract(this.addForm.value)
    .subscribe(data => {
      alert('Contract created successfully');
      // Reset Form      
      this.addForm.reset();
    });
}

Upvotes: 0

Related Questions