I already have this code:
<body ng-keydown="key($event);" ng-controller="appController">
function appController($scope) {
$scope.key = function($event){
console.log($event.keyCode);
if ($event.keyCode == 38)
alert("up arrow");
else if ($event.keyCode == 39)
alert("right arrow");
else if ($event.keyCode == 40)
alert("down arrow");
else if ($event.keyCode == 37)
alert("left arrow");
}
}
But I have just one controller that when it is present inside of the body will need to call a function if the arrow keys are pressed.
How can I connect they events in the body from my appController down to the lower level controller which in this case is called questionController?
If you are maintaining event on you main parent controller then $broadcast event to their children's inside $scope
.
$scope.key = function($event) {
$scope.$broadcast('key', $event)
}
$scope.$on = function(event, keyEvent) {
console.log($event.keyCode);
if (keyEvent.keyCode == 38)
alert("up arrow");
else if (keyEventt.keyCode == 39)
alert("right arrow");
else if (keyEvent.keyCode == 40)
alert("down arrow");
else if (keyEvent.keyCode == 37)
alert("left arrow");
}
No need use $broadcast
inside $rootScope
, because all the child scope will listen the this event.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments