1

I have it running fine on safari right now and not always working in chrome:

app.controller(.... function($window){
  $window.focus();

  $window.onfocus = function(){
    $scope.inFocus = true;
  };

  $window.onblur = function (){
    console.log("onblur");
    $scope.inFocus = false;
  };

});

The console.log is not always triggered. I was wondering if that was a bug or something I am doing wrong?

Update: I tried the solution given by @Gabe. It is still breaking sometimes, I am wondering if another action is interfering with it. It does look random though...

Update 2: I still don't know how it gets stuck, but I know how to get it unstuck:

  1. I click on an input in the window to focus it.
  2. I click outside of the input to unfocus.
  3. I switch tab and back in. It works again.
Alex C
  • 1,334
  • 2
  • 18
  • 41
  • possible duplicate of [onblur event not working with google chrome](http://stackoverflow.com/questions/2407679/onblur-event-not-working-with-google-chrome) – Jonathan Rowny Feb 18 '14 at 16:28
  • @JonathanRowny Your link says it s supported. Besides it does work 'sometimes' in chrome. Not sure what breaks it. – Alex C Feb 18 '14 at 16:34
  • where is the controller? I think maybe the events will get cleaned up if this is on a specific route. – Jonathan Rowny Feb 18 '14 at 16:35
  • @JonathanRowny this is the main controller for the homepage. I ll check if the routes affect it but I dont think they will. – Alex C Feb 18 '14 at 16:42

1 Answers1

0

Try using the API for angular.element instead

angular.element($window).bind('blur', function (){
  console.log("onblur");
  $scope.inFocus = false;
});

http://plnkr.co/edit/khYHeJlvn2uCzKeTjndM

Gabe
  • 2,117
  • 1
  • 16
  • 13
  • It seems to be working better. I ll see over the long term. Thanks :). – Alex C Feb 18 '14 at 17:39
  • Could you clarify what you mean by 'breaks sometimes'? – Gabe Feb 18 '14 at 18:17
  • when I switch tab, the onblur event is not triggered anymore. It happens randomly (at least I cant reproduce it in a predictable manner). Oh yeah, I should add that there is no error in the console. – Alex C Feb 18 '14 at 18:39