Allan Rodsmith
Allan Rodsmith

Reputation: 61

onChange handler doesn't work in Angular 6

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

Answers (1)

Ashutosh Kushawaha
Ashutosh Kushawaha

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

Related Questions