Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Angular2 md-autocomplete : How to manage onkeydown?

I implemented Angular2 material autocomplete in my app. And I am now looking for a way to manage the keydown.enter function properly.

Because the click function below works the way I expect, I naturally tried :

<md-input-container>
 <input type="text" placeholder={{appName}} mdInput [formControl]="term" [mdAutocomplete]="auto">
 </md-input-container>
<md-autocomplete #auto="mdAutocomplete" md-focus>
 <md-option *ngFor="let item of items" [value]=item.title (click)="loadData(item)" (keydown.enter)="loadData(item)">              
   <span>{{item.title}}</span>
 </md-option>
</md-autocomplete>

But the function is not triggered. If I move the keydown.enter function in md-input-container, like this :

<md-input-container >
 <input type="text" placeholder={{appName}} mdInput [formControl]="term" [mdAutocomplete]="auto" (keydown.enter)="loadData(item)">
</md-input-container>
<md-autocomplete #auto="mdAutocomplete" md-focus>
 <md-option *ngFor="let item of items" [value]=item.title (click)="loadData(item)">              
  <span>{{item.title}}</span>
 </md-option>
</md-autocomplete>

It works, but then I don't have access to my item element anymore...

I am sure there is a simple way around this, but I haven't been able to figure it out. How can I get my item object within md-input-container ?

Here is a Plunker.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can get the selected item in md-option by calling (onSelect)="loadData(item)" instead of the 'click' and 'keydown' functions.

Edit : As pointed out by Joshua below, you should now call onSelectionChange event.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!