我在角度6中执行(ngSubmit)后,表单没有刷新

时间:2018-09-04 09:52:25

标签: angular angular6

下面是app.component.html的代码段

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

app.component.ts

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

我的问题是收到警报后,表单值应自动清除,即应刷新表单。对我来说这不是真的。任何建议都是可取的。

2 个答案:

答案 0 :(得分:1)

点击“确定”后,您必须清除表单

这是一个例子

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

    });
}

您也可以使用window.confirm

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

答案 1 :(得分:0)

您需要调用表单的reset()方法。

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