angular.js监听键preSS作为快捷方式按钮 [英] angular.js listen for keypress as shortcut for button

查看:490
本文介绍了angular.js监听键preSS作为快捷方式按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个角应用是pretty基本测量工具。我有选择题,以及每个答案按钮和基本功能的登录按钮点击这样每个答案:

My first ever angular application is a pretty basic survey tool. I have multiple choice questions, with a button for each answer and a basic function that logs each answer on button click like this:

ng-click="logAnswer(answer.id)"

我正在寻找的是要能够密钥preSS事件添加到将侦听它与按钮选择和相匹配的1一个键盘响应,2,3,4,5的文件调用相同的功能。

What I'm looking for is to be able to add a keypress event to the document that will listen for a keyboard response of 1, 2, 3, 4, 5 which matches up with the button choices and calls the same function.

在四周寻找我唯一能找到,涉及到关键的presses一旦特定输入字段具有焦点,这不利于我的反应。我发现这个帖子上的有机磷农药的响应<一个href=\"http://stackoverflow.com/questions/20986240/angular-js-key$p$pss-events-and-factories\">Angular.js关键preSS事件和这似乎朝着正确的方向前进工厂,但我就是想不通,我怎么得到他的指示打电话给我的功能。

In searching around I can only find responses that relate to keypresses once a particular input field has focus, which doesn't help me. I did find the OPs response on this post Angular.js keypress events and factories which seems to be heading in the right direction, but I just can't work out how I get his directive to call my function.

我已经包括指令在我的JS:

I've included the directive in my js:

angular.module('keypress', []).directive('keypressEvents', 
  function($document, $rootScope) {
    return {
      restrict: 'A',
      link: function() {
        $document.bind('keypress', function(e) {
           $rootScope.$broadcast('keypress',e , String.fromCharCode(e.which));
        });
     }
   }
})

但我不知道我怎么做我的控制器中使用的按键绑定的对象:

but I'm not sure how I make use of the keybinding object within my controller:

function keyedS(key, parent_evt, evt){
      // key is the key that was pressed
      // parent_evt is the keypress event
      // evt is the focused element object
}
$scope.keyBindings = {
    's': keyedS
}

如何让我的按键绑定对象监听键,我听的和火灾关闭我需要的功能?

How do I make the keybinding object listen for the keys I'm listening for and fire-off the function that I need?

感谢

推荐答案

抓住了rootscope在你的控制器发出事件:

Catch the event emitted by the rootscope in your controller:

$rootScope.$on('keypress', function (e, a, key) {
    $scope.$apply(function () {
        $scope.key = key;
    });
})

然后你在控制器中使用。

key is then yours to use in the controller.

下面是一个小提琴

这篇关于angular.js监听键preSS作为快捷方式按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆