Created
July 28, 2013 02:03
-
-
Save snahor/6097040 to your computer and use it in GitHub Desktop.
This file contains 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
'use strict'; | |
var onlineStatusApp = angular.module('onlineStatusApp', []); | |
onlineStatusApp.factory('onlineStatus', ["$window", "$rootScope", function ($window, $rootScope) { | |
var onlineStatus = {}; | |
onlineStatus.onLine = $window.navigator.onLine; | |
onlineStatus.isOnline = function() { | |
return onlineStatus.onLine; | |
} | |
$window.addEventListener("online", function () { | |
onlineStatus.onLine = true; | |
$rootScope.$digest(); | |
}, true); | |
$window.addEventListener("offline", function () { | |
onlineStatus.onLine = false; | |
$rootScope.$digest(); | |
}, true); | |
return onlineStatus; | |
}]); | |
onlineStatusApp.controller( 'OnlineStatusCtrl', function OnlineStatusCtrl( $scope, onlineStatus ) { | |
$scope.onlineStatus = onlineStatus; | |
$scope.$watch('onlineStatus.isOnline()', function(online) { | |
$scope.online_status_string = online ? 'online' : 'offline'; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment