Difference between revisions of "Checking online status in AngularJS"

From Wiki2
Line 1: Line 1:
==Checking online status in AngularJS app==
==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.
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.
Line 10: Line 9:
Interceptors are set up in the app.config.
Interceptors are set up in the app.config.


  var app = angular.module("App", []);
  app.config(function ($httpProvider) {
      $httpProvider.interceptors.push('Interceptor');
  });





Revision as of 12:19, 28 August 2014

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.

 var app = angular.module("App", []);
 app.config(function ($httpProvider) {
     $httpProvider.interceptors.push('Interceptor');
 });


$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