簡體   English   中英

ng-keydown不適用於angular

[英]ng-keydown doesn't work in 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

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

版本1.1.1不支持ngKeydown指令。 您應該使用atleast版本1.1.2來使用此偽指令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM