In response to Christie Bercchan, he suggests assigning the function $ window.onfocus, which works. However, there is a problem with this ... only one function can be assigned to $ window.focus at a time. Thus, by assigning the function $ window.onfocus, you can accidentally overwrite the previous function, and your function will also be vulnerable to being overwritten later.
Here is another solution that allows you to run several functions with window focus or blur events:
var onFocus = function () { // do something }; var onBlur = function () { // do something else }; var win = angular.element($window); win.on("focus", onFocus); win.on("blur", onBlur);
This will allow you to assign several functions to the focus and blur events for the $ window object.
If you added functions inside the controller and want to remove these functions when the controller is destroyed, you can do something like this:
$scope.$on("$destroy", function handler() { win.off("focus", onFocus); win.off("blur", onBlur); $interval.cancel(interval); });
Solution inspired by this post: https://www.bennadel.com/blog/2934-handling-window-blur-and-focus-events-in-angularjs.htm
source share