如何重置 Angular 响应式表单
How can I reset Angular Reactive Form
我已经尝试过但未能找到重置我的 angular 表单的方法。
有人可以帮忙吗?
<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();
}
}
差不多!为此使用反应形式:
<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();
}
}
<form [formGroup]="thisIsAForm" (ngSubmit)="onSubmit()">
<mat-form-field class="full-width">
<input formControlName="weather" placeholder="Weather">
</mat-form-field>
</form>
<button mat-raised-button (click)="resetForm()">Reset</button>
export class Example{
thisIsAForm: FormGroup;
constructor() {
this.thisIsAForm = new FormGroup(
weather: new FormControl('')
);
}
resetForm() {
this.thisIsAForm.reset();
}
}
这里我要重置 template-driven 表单而不会使表单变脏。
<form class="example-form" #charreplaceform="ngForm">
<mat-form-field class="example-full-width">
<input matInput #codepointSel="ngModel" (change)="createReplacementChar()" (keyup)="checkForNumber()"
required [(ngModel)]="charReplaceInfo.codePoint" name="code-point" placeholder="Code Point (Only number)"
[disabled]="isDisabled">
</mat-form-field>
<a (click)="refreshCharRecord(charreplaceform)">
<i class="material-icons">
loop
</i>
</a>
</form>
// in .ts
refreshCharRecord(form: NgForm) { // getting the form reference from template
form.form.reset(); // here we are resetting the form without making form dirty
}
在Angular8,如果你这样做
<form #form="ngForm" (ngSubmit)="process(form)">
process(form: NgForm) { ...
使用 --prod
构建时会出现以下错误
Argument of type 'FormGroupDirective' is not assignable to parameter
of type 'NgForm'.
在 (ngSubmit)
我所做的是将 form
模板引用注入为 FormGroupDirective
然后调用 resetForm()
.
<form #form (ngSubmit)="process(form)">
@ViewChild('form', { static: false }) FormGroupDirective formDirective;
我认为您需要在 html 代码中添加 ngForm 指令,如下所述,stackbliz 示例还将名称和 ngModel ngControl 添加到表单元素中。
对我来说,向重置按钮添加 type="reset" 解决了问题
<button **type = "reset"** mat-raised-button (click)="resetForm()">Reset</button>
我已经尝试过但未能找到重置我的 angular 表单的方法。
有人可以帮忙吗?
<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();
}
}
差不多!为此使用反应形式:
<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();
}
}
<form [formGroup]="thisIsAForm" (ngSubmit)="onSubmit()">
<mat-form-field class="full-width">
<input formControlName="weather" placeholder="Weather">
</mat-form-field>
</form>
<button mat-raised-button (click)="resetForm()">Reset</button>
export class Example{
thisIsAForm: FormGroup;
constructor() {
this.thisIsAForm = new FormGroup(
weather: new FormControl('')
);
}
resetForm() {
this.thisIsAForm.reset();
}
}
这里我要重置 template-driven 表单而不会使表单变脏。
<form class="example-form" #charreplaceform="ngForm">
<mat-form-field class="example-full-width">
<input matInput #codepointSel="ngModel" (change)="createReplacementChar()" (keyup)="checkForNumber()"
required [(ngModel)]="charReplaceInfo.codePoint" name="code-point" placeholder="Code Point (Only number)"
[disabled]="isDisabled">
</mat-form-field>
<a (click)="refreshCharRecord(charreplaceform)">
<i class="material-icons">
loop
</i>
</a>
</form>
// in .ts
refreshCharRecord(form: NgForm) { // getting the form reference from template
form.form.reset(); // here we are resetting the form without making form dirty
}
在Angular8,如果你这样做
<form #form="ngForm" (ngSubmit)="process(form)">
process(form: NgForm) { ...
使用 --prod
Argument of type 'FormGroupDirective' is not assignable to parameter of type 'NgForm'.
在 (ngSubmit)
我所做的是将 form
模板引用注入为 FormGroupDirective
然后调用 resetForm()
.
<form #form (ngSubmit)="process(form)">
@ViewChild('form', { static: false }) FormGroupDirective formDirective;
我认为您需要在 html 代码中添加 ngForm 指令,如下所述,stackbliz 示例还将名称和 ngModel ngControl 添加到表单元素中。
对我来说,向重置按钮添加 type="reset" 解决了问题
<button **type = "reset"** mat-raised-button (click)="resetForm()">Reset</button>