简体   繁体   English

ng-keydown不适用于angular

[英]ng-keydown doesn't work in angular

I try to bind arrow keydown event to the document, but it doesn't work in angular. 我尝试将箭头keydown事件绑定到文档,但是它在角度上不起作用。 code: 码:

function CubeCtrl($scope, $locale) {

$scope.click = function(){
    alert("click")
}
$scope.keydown = function(){
    alert("keydown")
}

} }

html: HTML:

<body ng-app ng-controller="CubeCtrl" ng-click="click()" ng-keydown="keydown()">

and here is jsfiddle 这是jsfiddle

This is probably Angular version issue. 这可能是Angular版本问题。

You can chcek this solution: UI Utils 您可以选择以下解决方案: UI Utils

Or best way is to write your own directive for this event: 或最好的方法是为此事件编写自己的指令:

Directive: 指示:

   var mod = angular.module('mydirectives');
mod.directive('ngKeydown', function() {
    return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
             // this next line will convert the string
             // function name into an actual function
             var functionToCall = scope.$eval(attrs.ngKeydown);
             elem.on('keydown', function(e){
                  // on the keydown event, call my function
                  // and pass it the keycode of the key
                  // that was pressed
                  // ex: if ENTER was pressed, e.which == 13
                  functionToCall(e.which);
             });
        }
    };
});

HTML HTML

<input type="text" ng-keydown="onKeydown">

The ngKeydown directive is not supported by version 1.1.1. 版本1.1.1不支持ngKeydown指令。 You should be using atleast version 1.1.2 for using this directive. 您应该使用atleast版本1.1.2来使用此伪指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM