Created
August 8, 2016 20:17
-
-
Save wwwmarcos/e907ae2c90e2518f7b0f209da550766d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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