本文介绍了如何重置Angular反应形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试过但未能找到重设角度形状的方法.
I have tried and failed to find the way in which to reset my angular form.
有人可以帮忙吗?
<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();
}
}
这篇关于如何重置Angular反应形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!