0
我有兩個組件,一個是員工,另一個是部門。我想做一個下拉,以便員工可以選擇一個部門。Angular 2 JS下拉菜單,從另一個組件選擇項目
我實現了一個方法,讓我的員工組件內部各部門:
getAllDepartment(){
this._employeeService.getAllDepartment()
.subscribe(data => this.departments = data.json());
}
在我的員工HTML選擇看起來是這樣的:
<label for="editTitle">Department:</label><br>
<select [(ngModel)]="newItem.Department" (click)="getAll()" style="width: 180px">
<option *ngFor="#department of departments" [value]="department.departmentNo">{{department.departmentName}}</option>
</select>
但注意到發生。
使下拉工作和選擇項目的最佳方式是什麼? 這是ahtml在那裏我有選擇:
<form #f="ngForm" [hidden]="!showAddView" align="center">
<div>
<label for="editTitle">Employee No:</label><br>
<input type="text" [(ngModel)]="newItem.employeeNo" ngControl="employeeNo" required >
</div>
<div>
<label for="editAbrv">Employee name:</label><br>
<input type="text" [(ngModel)]="newItem.employeeName" ngControl="demployeeName" required >
</div>
<div>
<label for="editTitle">Department:</label><br>
<select [(ngModel)]="newItem.departmentNo" (click)="getAll()" style="width: 180px">
<option *ngFor="#department of departments" [value]="department.departmentNo">{{department.departmentName}}</option>
</select>
</div>
<div>
<label for="editAbrv">Salary:</label><br>
<input type="text" [(ngModel)]="newItem.salary" ngControl="salary" required>
</div>
<div>
<a href="javascript:void(0);" (click)="addEmployee(newItem)" title="Add">
Save
</a>
<a href="javascript:void(0);" (click)="showHide($event)" >
Cancel
</a>
</div>
</form>
Omg,我是盲人!謝謝,它現在可行! :D –
但是,我遇到了一個問題,當我從下拉菜單中選擇某項時,項目未被選中(我可以在桌面上看到它)。 –
我不清楚這個問題。根據具體情況創建另一個問題。 – gamesmad