Angular Catch a Global Click

There are so many answers to this question, but I cannot figure out how to solve two problems. After research, I build a very simple directive.

.directive('keypressEvents', function ($document) {
    return {
        restrict: 'A',
        link: function () {
            $document.bind('keypress', function (e) {
                alert(e.keyCode);
            });
        }
    }
});

The first problem is more like a question, if I did this angular.service('myService', myServiceFunction);, would it work globally? Secondly, some keys do not work, like ESC, ctrl arrows, etc. I am working on this CODEPEN

+4
source share
2 answers

, $document. . , . - , , , , $ .

, , . , , , , , .

Esc , Esc . keydown keyup.

- keyup.

$document.bind('keyup', function (e) {
    alert(e.keyCode);
});

, .

+7

, - CODEPEN

angular.module('myApp', [])
.config(function () {
  angular.element(document).bind('keyup', function (e) {
    alert(e.keyCode);
  });
});
+4

Source: https://habr.com/ru/post/1606787/


All Articles