Difference between revisions of "Checking online status in AngularJS"
Line 8: | Line 8: | ||
===Intercepting http calls=== | ===Intercepting http calls=== | ||
Interceptors are set up in the app.config. | Interceptors are set up in the app.config. | ||
< | <syntaxhighlight> | ||
var app = angular.module("App", []); | var app = angular.module("App", []); | ||
Line 14: | Line 14: | ||
$httpProvider.interceptors.push('Interceptor'); | $httpProvider.interceptors.push('Interceptor'); | ||
}); | }); | ||
</ | </syntaxhighlight> | ||
Revision as of 12:24, 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. <syntaxhighlight> var app = angular.module("App", []);
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('Interceptor');
}); </syntaxhighlight>
$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