是否可以使用一个按钮清除/重置表单?表单包含材料datepicker,input,textarea,select和checkboxes。
我的实际代码:
<form class="example-form" #form="ngForm">
<button mat-raised-button color="primary (click)="clear(form.value)">Clear</button>
<mat-form-field class="example-full-width">
<input matInput placeholder="Name" name="name" ngModel>
</mat-form-field>
<mat-form-field>
<input matInput [matDatepicker]="birthday" [(ngModel)]="geburtsdatum" placeholder="Geburtsdatum" name="geburtsdatum">
<mat-datepicker-toggle matSuffix [for]="birthday"></mat-datepicker-toggle>
<mat-datepicker #birthday></mat-datepicker>
</mat-form-field>
<mat-checkbox [(ngModel)]="unlimited" name="unbegrenzt">unlimited</mat-checkbox>
<mat-form-field>
<mat-select placeholder="Transpondertyp" name="transponderTyp" [(ngModel)]="form.transponderTyp">
<mat-option>None</mat-option>
<mat-option *ngFor="let transponderTyp of transponderTyps" [value]="transponderTyp.value">
{{ transponderTyp.viewValue }}
</mat-option>
</mat-select>
</mat-form-field>
</form>
提前致谢: - )
祝你好运
答案 0 :(得分:1)
<button (click)="form.reset()" >RESET</button>
使用TD表格的reset()方法
答案 1 :(得分:1)
Suhag的解决方案应该可以工作,尽管我总是指定按钮类型,因此它不被视为提交按钮:
<button type="button"(click)="form.reset()">Reset</button>
否则,您可以简单地执行以下操作:
<button type="reset">Reset</button>
答案 2 :(得分:0)
您必须在点击事件中清除模型:
<form class="example-form" #form="ngForm">
<button mat-raised-button color="primary (click)="clear(form.value)">Clear</button>
<mat-form-field class="example-full-width">
<input matInput placeholder="Name" name="name" ngModel>
</mat-form-field>
<mat-form-field>
<input matInput [matDatepicker]="birthday" [(ngModel)]="geburtsdatum" placeholder="Geburtsdatum" name="geburtsdatum">
<mat-datepicker-toggle matSuffix [for]="birthday"></mat-datepicker-toggle>
<mat-datepicker #birthday></mat-datepicker>
</mat-form-field>
<mat-checkbox [(ngModel)]="unlimited" name="unbegrenzt">unlimited</mat-checkbox>
<mat-form-field>
<mat-select placeholder="Transpondertyp" name="transponderTyp" [(ngModel)]="form.transponderTyp">
<mat-option>None</mat-option>
<mat-option *ngFor="let transponderTyp of transponderTyps" [value]="transponderTyp.value">
{{ transponderTyp.viewValue }}
</mat-option>
</mat-select>
</mat-form-field>
<button (click)="resetForm()" >RESET</button>
</form>
成分:
resetForm() {
this.geburtsdatum = '';
this.unlimited = '';
this.form.transponderTyp = '';
this.transponderTyp.value = '';
}
答案 3 :(得分:0)