Skip to content

Instantly share code, notes, and snippets.

@wwwmarcos
Created August 8, 2016 20:17
Show Gist options
  • Save wwwmarcos/e907ae2c90e2518f7b0f209da550766d to your computer and use it in GitHub Desktop.
Save wwwmarcos/e907ae2c90e2518f7b0f209da550766d to your computer and use it in GitHub Desktop.
(function() {
'use strict';
/**
* @desc show network status when offline.
* @example <network-state message="No conection"></network-state>
*/
angular
.module('NetworkStateDirective', [])
.directive('networkState', networkState);
networkState.$inject = [];
function networkState(){
var directive = {
restrict : 'E',
scope: {
message : '@'
},
template: '<div data-ng-if="isOffline()">{{message}}</div>',
link: link
};
return directive;
function link(scope, element, attrs){
scope.isOffline = isOffline;
function isOffline(){
return !window.navigator.onLine;
};
window.addEventListener('online', function () {
scope.$digest();
});
window.addEventListener('offline', function () {
scope.$digest();
});
};
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment