Reputation: 61
I'm working on an Angular 6 project. I have this dropdown in my template which render the dropdown correctly:
<select id="companydropdown" onChange="getcompanyid(this)">
<option *ngFor="let company of filteredCompanies" value={{company.companyName}}
id={{company.id}}>
{{company.companyName}}
</option>
</select>
and in my component
getcompanyid(s)
{
var id = console.log(s[s.selectedIndex].id);
alert(id);
}
But the method never fires
Upvotes: 0
Views: 214
Reputation: 791
//This may help you.
<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.
}
Upvotes: 1