Donovant
Donovant

Reputation: 3161

How can I do an window key event with Angularjs to set an angular variable

I don't know how I can intercept a keydown event of the window and set from there an angular var into the scope, of a specify controller.

Upvotes: 1

Views: 1312

Answers (2)

Zack Argyle
Zack Argyle

Reputation: 8427

Here is a simple way to do it from your controller.

angular.element(window).bind("keydown",keydown);
function keydown(e) {
    $scope.eventKeydown = e.keyCode;
    $scope.$apply();
}

Upvotes: 1

Harutyun Abgaryan
Harutyun Abgaryan

Reputation: 2023

You can use this:

window.onkeydown = keydown;

function keydown(e) {
    $scope.eventKeydown = e;
    console.log(e);
    console.log($scope.eventKeydown);
}

Upvotes: 0

Related Questions