How to use a keypress event in AngularJS?
How to use a keypress event in AngularJS?
531
17-Jun-2024
Updated on 19-Jun-2024
Ashutosh Kumar Verma
19-Jun-2024Keypress Event
In AngularJS, you can use the built-in directive
ng-keypressto handle the keypress event.Here are the steps to use this event,
HTML
To add the keypress event in the angular application, you can use the
ng-keypressas given below,In the above syntax
ng-keypressis the built-in directive that triggers thehandleKeypressfunction when a key/button is pressed.$eventis the special type of angular expression that passes the event object to the function.JavaScript
Here's how to control for keypress events in the Angular JS application,
handleKeypressis the function defined in the controller scope that is called every time the key/button is pressed.eventis the event object that contains the information about the keypress(event.key)event.Example-
let's take a simple example that explain the use of keypress event in angular js,
<input type="text" ng-keypress="handleKeypress($event)">: The input field that triggers thehandleKeypressfunction every time when a key is pressed.Output-
Also, Read: How to display length of filtered ng-repeat data?