I have a global method for watching keypresses throughout my application. When a keypress happens, I broadcast the event like so:
var keycodes = {
'escape': 27,
}
angular.element($window).on('keydown', function(e) {
for (var name in keycodes) {
if (e.keyCode === keycodes[name]) {
$rootScope.$broadcast('keydown-' + name, angular.element(e.target))
}
}
})
This all works fine. In my controller, I listen for the event like so:
$rootScope.$on('keydown-escape', $scope.hideOverlays)
This also works fine, however, when I try to update attributes on the $scope object, I do not see the correct behavior in the DOM:
$scope.hideOverlays = function() {
for (var i=0; i < $scope.gallery.items.length; i++) {
$scope.gallery.items[i].overlayShown = false;
$scope.gallery.items[i].editing = false;
}
}
When I call this method internally from the controller, everything works fine, so I'm wondering if there's something that Angular is doing differently based on how the method is being called. I've tried called $scope.$apply(). In addition to seeming like that's not the right thing to do, I also get an error, so no dice there. Any help is greatly appreciated!