AngularJS NG-的keydown指令只工作了<输入>背景? [英] AngularJS ng-keydown directive only working for <input> context?

查看:354
本文介绍了AngularJS NG-的keydown指令只工作了<输入>背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新AngularJS但发现很合我的胃口至今。对于我目前的项目,我需要的热键功能,并很高兴地看到,这是自1.1.2版本的支持。

在NG-的keydown指令(<一个href=\"http://$c$c.angularjs.org/1.1.3/docs/api/ng.directive:ngKeydown\">http://$c$c.angularjs.org/1.1.3/docs/api/ng.directive:ngKeydown)按预期工作输入类型,但失败我要其他任何情况下像DIV等,这些似乎很奇怪,因为该文档,否则说。

下面是分别是一个小例子( http://jsfiddle.net/TdXWW/12/ )的工作不工作:

 &LT;输入NG-的keydown =键preSS($事件)&GT;
&LT; D​​IV NG-的keydown =键preSS($事件)&GT;

注:我知道这可能是与普通的jQuery(处理<一个href=\"http://www.mkyong.com/jquery/how-to-check-if-an-enter-key-is-$p$pssed-with-jquery/\">http://www.mkyong.com/jquery/how-to-check-if-an-enter-key-is-$p$pssed-with-jquery/)但我更preFER了解如何在AngularJS对付它。


解决方案

我有同样的问题,并能够按照此意见提供了这个简单的提示进行修复:的 http://stackoverflow.com/a/1718035/80264


  

您需要给DIV在tabIndex所以它可以接收焦点。


 &LT; D​​IV ID =testdiv的tabindex =0&GT;&LT; / DIV&GT;

I am pretty new to AngularJS but found it quite to my liking so far. For my current project I need hotkey functionality and was happy to see that it is supported since the 1.1.2 release.

The ng-keydown directive (http://code.angularjs.org/1.1.3/docs/api/ng.directive:ngKeydown) works as expected for input types but fails me for any other context like div etc. which seems odd given that the documentation says otherwise.

Here is an minimal example (http://jsfiddle.net/TdXWW/12/) of the working respectively the not working:

<input ng-keydown="keypress($event)">
<div ng-keydown="keypress($event)">

NOTE: I know this could be handled with plain jQuery (http://www.mkyong.com/jquery/how-to-check-if-an-enter-key-is-pressed-with-jquery/) but I much prefer to understand how to deal with it in AngularJS.

解决方案

I was having the same problem and was able to fix it by following this simple tip provided in this comment: http://stackoverflow.com/a/1718035/80264

You need to give the div a tabindex so it can receive focus.

<div id="testdiv" tabindex="0"></div>

这篇关于AngularJS NG-的keydown指令只工作了&LT;输入&GT;背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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