1

I have list of elements:

<div class="List">
<div class=" SearchListItem" [ngClass]="{ active: i == activeIndexResultItem }"
          *ngFor="let item of searchShortResultItems; let i = index"
          (mouseenter)="mouseEnterItem($event, i)" (keydown.enter)="searchByEnter($event, item)"
          (click)="selectSearchResult(item)"> {{ item.text }} 
        </div>
</div>

Why (keydown.enter) does not work?

searchByEnter(e, item) {
   console.log(e); // no invoke
} 

Also I have tried to change to:

(keydown)="searchByEnter($event, item)"

The same result

Also I havea set focus on: <divclass="List"#searchList>:

 if (this.searchList) {
      setTimeout(() => this.searchList.nativeElement.focus(), 50);
}
POV
  • 11,293
  • 34
  • 107
  • 201

1 Answers1

1

Keyboard events are only dispatched on focused elements. You can add tabindex="0" to make your DIV focusable.

If your parent element is focused and you just keep track of current element from the list — add (keydown.enter) to parent element and pass your active item to that handler.

waterplea
  • 3,462
  • 5
  • 31
  • 47