9

I'm expecting someone is going to suggest I use jQuery (which I have loaded) but just to be sure before I look into how to implement can someone tell me if there is another way that I can make the hitting of a key (the right arrow key) call a javascript function in my controller.

2 Answers2

21

If you want to listen to events on the document

  • register an event listener when the scope is instantiated
  • make sure you also destroy it when the scope is destroyed
  • I also tried to use keypress but it seems to ignore the arrow keys.
  • Read more: jQuery Keypress Arrow Keys
  • Maybe you'll need to use e.which: Javascript .keyCode vs. .which?
var handler = function(e){
    if(e.keyCode === 39) {
      console.log('right arrow');
      // $scope.doSomething();
    }      
};

var $doc = angular.element(document);

$doc.on('keydown', handler);
$scope.$on('$destroy',function(){
  $doc.off('keydown', handler);
})

What about ng-keypress / ng-keydown :

  • ng-keypress has a the same problem with arrow keys ( It just uses element.on('keypress',).
  • ng-keydown will probably work as well for you.

What is the angular way?

  • "Angular way" is a subjective term, although most angular experts would agree to some level.
  • Sometimes, angular let you choose between different ways and they are all fine.
  • It always depends on how your app is structured and what you want to achieve.
  • Angular.js provides some toys for convenience ( the built-in directives ), it doesn't mean you should always prefer using them.
  • My guidelines are decoupled / simple / clean code as much as possible.
  • Declarative not always means decoupled / simple / clean ( hmm... XML )
  • If your whole code lives inside a controller so why decouple it with a specific template that must declare ng-event with a specific function?!
  • I would always consider creating custom directives for events & handlers (but again it depends on the use case).
  • As you master angular.js (learn the internals) you will find "your own angular way".
Community
  • 1
  • 1
Ilan Frumer
  • 32,059
  • 8
  • 70
  • 84
  • 1
    Is this better than using ng-keypress? I am thinking ng-keypress may be a more "Angular" way of doing it. Can you let me know your opion? Thanks –  Jan 17 '14 at 04:32
1

You could use ng-keypress="callFunction($event)"

And in function

$scope.callFunction = function(eventNew) {
  if (eventNew.which==39)
    alert('Right arrow key pressed');
}
Jayram
  • 18,820
  • 6
  • 51
  • 68
  • Is ng-keypress new. I saw the link pointed to in the comment and did not see mention of it. –  Jan 16 '14 at 18:05
  • Where would you suggest I place this directive if I want to catch the keypress on any part of the page? –  Jan 16 '14 at 18:08
  • 1
    You can place it on the body. Like – Jayram Jan 16 '14 at 18:10
  • 1
    What if this is specific only to a specific part of the page? e.g. template within a modal popup. Any way to keep it clean and together with the part it is relevant to? – shim Jun 30 '16 at 22:44