我正在从事Angular 6项目。 我的模板中有此下拉菜单,可以正确呈现下拉菜单:
<select id="companydropdown" onChange="getcompanyid(this)">
<option *ngFor="let company of filteredCompanies" value={{company.companyName}}
id={{company.id}}>
{{company.companyName}}
</option>
</select>
以及在我的组件中
getcompanyid(s)
{
var id = console.log(s[s.selectedIndex].id);
alert(id);
}
但是该方法永远不会触发
答案 0 :(得分:1)
//这可能会对您有所帮助。
<select id="id" name="id" required #id="ngModel" [(ngModel)]="company.id" class="form-control" (change)="getcompanyid(company.id)" >
<option [ngValue]="null">Select company Name</option>
<option *ngFor="let dept of filteredCompanies" [value]="dept.id">
{{dept.companyName}}
</option>
</select>
//in component
getcompanyid(id:any)
{
alert(id);
//do some stuff with this id.
}