0

我尝试将箭头 keydown 事件绑定到文档,但它在 Angular 中不起作用。代码:

function CubeCtrl($scope, $locale) {

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

}

html:

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

这是jsfiddle

4

2 回答 2

1

这可能是 Angular 版本问题。

您可以查看此解决方案:UI Utils

或者最好的方法是为此事件编写自己的指令:

指示:

   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

<input type="text" ng-keydown="onKeydown">
于 2013-10-01T10:34:08.137 回答
0

The ngKeydown directive is not supported by version 1.1.1. You should be using atleast version 1.1.2 for using this directive.

于 2013-10-01T10:26:48.953 回答