How to use a keypress, keydown event on enter key press in AngularJS?

edx
edx
506 Points
24 Posts
<input ng-model="inputdata" ng-keydown="searchItem()">

I want to call searchItem() method on when enter key press.

Views: 26279
Total Answered: 2
Total Marked As Answer: 1
Posted On: 19-Sep-2017 03:03

Share:   fb twitter linkedin
Answers
beginer
beginer
1544 Points
52 Posts
         

User keycode:

<input ng-model="query" ng-keydown="$event.keyCode === 13 && searchItem()">
Posted On: 19-Sep-2017 03:09
Smith
Smith
2890 Points
78 Posts
         
<input ng-model="inputdata" ng-keydown="searchItem()">

Now,  let's add keyboard support.

trigger by enter key:

<input ng-model="inputdata" 
ng-keydown="$event.keyCode === 13 && searchItem()">

by space key:

<input ng-model="inputdata" 
ng-keydown="$event.keyCode === 32 && searchItem()">

by space or enter key:

<input ng-model="inputdata" 
ng-keydown="($event.keyCode === 13 || $event.keyCode === 32) && searchItem()">

if you are in modern browser

<input ng-model="inputdata" 
ng-keydown="[13, 32].includes($event.keyCode) && searchItem()">
Posted On: 19-Sep-2017 03:53
great man!
 - nik  11-May-2018 20:36
 Log In to Chat