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

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

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

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

Share:   fb twitter linkedin
Answers
beginer
beginer
68 Points
29 Posts
     

User keycode:

<input ng-model="query" ng-keydown="$event.keyCode === 13 && searchItem()">
Posted On: 19-Sep-2017 03:09
Smith
Smith
Participant
360 Points
110 Posts
Marked as answer      
<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
banner

Blog

Active User (2)

 Log In to Chat