且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何重置Angular反应形式

更新时间:2023-12-02 20:04:04

差不多!为此,请使用反应形式:

Almost ! Use a reactive form for that :

<form [formGroup]="myForm">
  <mat-form-field class="full-width">
    <input matInput placeholder="Weather" formControlName="weather">
  </mat-form-field>
</form>
<button mat-raised-button (click)="myForm.reset()">Reset</button>


export class Example{
  myForm: FormGroup;

  constructor(private fb: FormBuilder) { 
    this.myForm = fb.group({
      weather: ''
    });
  }

  // If the HTML code doesn't work, simply call this function
  reset() {
    this.myForm.reset();
  }
}