0

I try to bind arrow keydown event to the document, but it doesn't work in angular. code:

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()">

and here is jsfiddle

1
  • It works fine with the unstable version of Angularjs which is a release candidate already. Here is your fiddle with it jsfiddle.net/ranru/sYAwC/2 Commented Oct 1, 2013 at 10:46

2 Answers 2

1

This is probably Angular version issue.

You can chcek this solution: 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

<input type="text" ng-keydown="onKeydown">
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.