Matheus Lima
Matheus Lima

Reputation: 2143

AngularJS check if user is in the tab

I'd like to check if the user is currently viewing the browser tab, like if you are at google chrome and you have facebook, twitter and the application, and you want to know if the application is selected between all 3. Is there a way to do it with purely with angular or do I need to do something like this:

$(window).focus(function() {
  console.log('enter');
}).blur(function() {
  console.log('out');
});

Upvotes: 0

Views: 2499

Answers (1)

SeanCannon
SeanCannon

Reputation: 77956

AngularJS comes bundled with JQLite (subset of jQuery). You can inject the mock $window to keep it testable or just use window. Your call :

angular.element($window).bind('focus', function() {
  console.log('enter');
}).bind('blur', function() {
  console.log('out');
});

Upvotes: 3

Related Questions