Checking online status in AngularJS

From Wiki2

Checking online status in AngularJS app

Often apps can do useful work while offline. While online and connected to the server additional functionality is enabled. It is helpful to know if a device is connected to the internet and if its server is available.

You might as well check online status every time your app makes an http request. If you wanted to periodically update your online status you could make a simple http request and intercept its response to set the state of a $rootscope variable that could then be watched by any controllers that needed to.

Intercepting http calls, setting some rootScope variables and watching them from a controller, and polling the server periodically to update everything are the steps described below.

Intercepting http calls

Interceptors are set up in the app.config.


$window.navigator.onLine would tell you if your device is online but not if the server is.

watching rootscape variables

interval polling of the server

tags: interceptors, polling, $interval