在选择选项中选择默认 [英] Selecting default in select option
问题描述
在我的Angular程序中,我有一个选择框,该框使用ngFor
显示许多员工,并使用ngValue
和ngModel
从列表中获取员工人数,并在访问时将其用作索引数组.我想在所选的所有员工上方都有一个选项.下面是我尝试过,只要选择所有员工或员工的一个一切正常,但我想默认是所有员工的选择.我该怎么办?
In my Angular program, I have a select box that uses an ngFor
to display many employees and uses the ngValue
and ngModel
to take the number of employee from the list and use it as an index when accessing an array. I want to have an option above all of the employees that's selected. Here's what I've tried, everything works whenever All Employees or one of the employees is chosen, but I want the default to be the All Employees option. How can I do that?
HTML
:
<select class="form-control" id="empName" [(ngModel)]="selectedEmployee">
<option selected>All Employees</option>
<option *ngFor="let emp of empInfo; let i = index" [ngValue]="i">{{emp.EmpID}} - {{emp.FirstName}} {{emp.LastName}}</option>
</select>
这是我的ts中的变量:
and here's the variable in my ts:
selectedEmployee: number;
我尝试了以下两种方法:
I've tried these two ways:
<option selected>All Employees</option>
<option selected="selected">All Employees</option>
但都不起作用.
推荐答案
与
[(ngModel)]="selectedEmployee"
class MyComponent {
selectedEmployee:number = 3;
您可以使用
(ngModelChange)="updateSelection($event)"
updateSelection(value) {
this.model.employeeNo = value;
}
这篇关于在选择选项中选择默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!