ng-keydown不适用于angular

鲁珀特

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

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

诺伯特·皮斯(Norbert Pisz)

这可能是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">

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章