First of all, I advise you to listen to online or offline events .
You can do this in AnguarJS:
var app = module('yourApp', []); app.run(function($window, $rootScope) { $rootScope.online = navigator.onLine; $window.addEventListener("offline", function() { $rootScope.$apply(function() { $rootScope.online = false; }); }, false); $window.addEventListener("online", function() { $rootScope.$apply(function() { $rootScope.online = true; }); }, false); });
NOTE. I am moving the change of the root region variable to the $apply method to notify Angular that something has been changed.
After that you can:
In controlller:
$scope.$watch('online', function(newStatus) { ... });
In the HTML markup:
<div ng-show="online">You're online</div> <div ng-hide="online">You're offline</div>
Here is a working Plunker: http://plnkr.co/edit/Q3LkiI7Cj4RWBNRLEJUA?p=preview
Another solution could be a live / offline event. But in this case, you need to initialize the current status at boot, and then subscribe to the event.
Valentyn Shybanov Apr 26 '13 at 18:12 2013-04-26 18:12
source share