The documentation for Angular's
ngkeyup
is virtually non-existant. I have a container div for my view that I want to apply key events to, but I want to control them from a function in my controller.
I'm trying to do something like this:
view:
<div class="container" ng-keyup="keyPress(e)"></div>
Controller:
$scope.keyPress = function(e){ console.log(e); // where 'e' is the keycode of whatever was pressed }
Is this possible at all? I can't find any information, anywhere, on what ng-keyup
actually does and how to use it! For instance, I want to trigger an event when the left arrow key (37)
is pressed, how would I achieve this with Angular?
AngularJS ng-keyup DirectiveThe ng-keyup directive tells AngularJS what to do when the keyboard is used on the specific HTML element. The ng-keyup directive from AngularJS will not override the element's original onkeyup event, both will be executed.
(keyup) is an Angular event binding to respond to any DOM event. It is a synchronous event that is triggered as the user is interacting with the text-based input controls. When a user presses and releases a key, the (keyup) event occurs.
you can easily use keypress event in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13 and angular 14 application. When user will press key on input box field then trigger onKeypressEvent() of angular component. we will use (change) attribute for call function.
AngularJS ng-change DirectiveThe ng-change event is triggered at every change in the value. It will not wait until all changes are made, or when the input field loses focus. The ng-change event is only triggered if there is a actual change in the input value, and not if the change was made from a JavaScript.
You can do something like, passing $event
object.
<div class="container" ng-keypress="keyPress($event)"></div>
This is mentioned in documentation
Expression to evaluate upon keyup. (Event object is available as $event and can be interrogated for keyCode, altKey, etc.)
Template:
<div class="container" ng-keyup="keyPress($event.keyCode)"></div>
Controller:
$scope.keyPress = function(keyCode){ console.log(keyCode); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With