如何重置在angular2中的表格中定义的下降值



我有一个定义为:的形式

<form id="form_details "#form="ngForm" (ngSubmit)="onSubmit(form.value)"  >

在这个表单中,我有1个下拉列表,当我点击表单上定义的提交按钮时,我可以得到下拉列表的值。我的要求是,我有另一个tge形式的按钮,它应该起到重置的作用,也就是说,当我点击它时,下拉列表应该返回到它的默认选择值。如何做到这一点?此外,当表单在重置后提交时,表单值应为所有员工。(不为空(。

<form id="form_details "#form="ngForm" (ngSubmit)="onSubmit(form.value)"  >
<select [(ngModel)]="emp_name" id='emp' name='emp_name'>
<option selected>All Employees</option>
<option *ngFor="let item of employees " value= {{emp}}>
{{emp}}
</option> 
</select>
</form>

还为默认选项分配一个值(如"All Employees"或"AllEmployee"等(。

.html

<form id="form_details "#form="ngForm" (ngSubmit)="onSubmit(form.value)"  >
<select [(ngModel)]="emp_name" id='emp' name='emp_name'>
<option selected value="All Employees">All Employees</option>
<option *ngFor="let item of employees " value= {{emp}}>
{{emp}}
</option> 
</select>


<button type="button" (click)="onReset()">Reset</button>
<button type="submit" >Submit</button>
</form>

.ts

onSubmit(value) {
this.emp_name = value;
}
onReset() {
this.emp_name = "All Employees";
}

最新更新