I have tried and failed to find the way in which to reset my angular form.
Can somebody help?
<form #thisIsAForm>
<mat-form-field class="full-width">
<input matInput placeholder="Weather">
</mat-form-field>
</form>
<button mat-raised-button (click)="resetForm()">Reset</button>
export class Example{
@ViewChild('thisIsAForm') thisIsAForm;
resetForm() {
this.thisIsAForm.reset();
}
}
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();
}
}