I am trying to rewrite the following controller as a service so that it can be called from my view within an ng-show
directive, but I am currently struggling. It needs to be a service so that it returns a true or false value, and can be reused all over the app:
module.controller('MyCtrl', function($rootScope, $cordovaNetwork)
document.addEventListener("deviceready", function () {
var type = $cordovaNetwork.getNetwork()
var isOnline = $cordovaNetwork.isOnline()
var isOffline = $cordovaNetwork.isOffline()
// listen for Online event
$rootScope.$on('$cordovaNetwork:online', function(event, networkState){
var onlineState = networkState;
})
// listen for Offline event
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
var offlineState = networkState;
})
}, false);
});
This is what I have done so far, which isn't working at the moment:
(function() {
'use strict';
angular
.module('dingocv.services')
.service('ConnectionService', ConnectionService)
function ConnectionService($rootScope, $cordovaNetwork) {
this.isOnline = function() {
var type = $cordovaNetwork.getNetwork()
var isOnline = $cordovaNetwork.isOnline()
var isOffline = $cordovaNetork.isOffline()
$rootScope.$on('$cordovaNetwork:online', function(event, networkState)) {
return networkState;
}
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState)) {
return networkState;
}
}
}
})();