Hi! I wonder, is there a way to bind document level keyup events on a specific view only?
I posted my problem on Stack overflow, but no solution yet :-/ http://stackoverflow.com/questions/25018422/what-is-a-correct-way-to-bind-document-level-key-events-in-angularjs-specific-on?noredirect=1 background: I have a single page app that opens a gallery. I want to bind document level keyup event (for keyboard gallery controlls) only when the gallery is open, ie. when route matches Demo (Click on "Fotografie" to open the gallery) http://tr.tomasreichmann.cz/ I have a jQuery background, so my instincts don't do me well. Anyway, binding the event on HTML through directive seems ... inellegant. Is there a way to bind it discreetly perhaps inside the controller code? I only need the event to register at a specific route. Thanks for your time and effort. It's greatly appreciated. On Wednesday, February 13, 2013 10:28:40 PM UTC+1, Christopher Martin wrote: > > Hi, I'm new to AngularJS and wondered if there was an alternate way of > capturing keyup/keydown events from the document level in my controllers. > > The solution I came up with works fine, but seemed like a roundabout way > of doing it. > > Using v1.1.2, I'm adding the ng-keyup directive to the <html> tag and > re-broadcasting the event: > > <html lang="en" ng-keyup="$broadcast('my:keyup', $event)"> > > And in my Controllers: > > $scope.$on('my:keyup', function(event, keyEvent) { > console.log('keyup', keyEvent); > }); > > I'm using the $routeProvider and ng-view to dynamically activate the > controllers. I had originally tried to call a $scope function (set up by > the controllers) from the ng-keyup directive but that didn't appear to > work - I suspect it's a difference with multiple scopes in play (<html> > outside the Controller/ng-view scope?)...still trying to wrap my head > around it all. > > Would it be possible to register an event listener using the $document > service directly? Curious about best practice for this case. > > Thanks! > -- You received this message because you are subscribed to the Google Groups "AngularJS" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/angular. For more options, visit https://groups.google.com/d/optout.
