methuselah methuselah - 1 month ago 20
AngularJS Question

TypeError: ConnectionService.isOnline is not a function

I keep the following error message when trying to instantiate my service in the controller:

ionic.bundle.js:26794 TypeError: ConnectionService.isOnline is not a function
at new MonitorService (http://localhost:8100/app/services/connection.service.js:33:24)
at Object.instantiate (http://localhost:8100/lib/ionic/js/ionic.bundle.js:18010:14)
at Object.<anonymous> (http://localhost:8100/lib/ionic/js/ionic.bundle.js:17850:24)
at Object.invoke (http://localhost:8100/lib/ionic/js/ionic.bundle.js:17995:19)
at Object.enforcedReturnValue [as $get] (http://localhost:8100/lib/ionic/js/ionic.bundle.js:17834:37)
at Object.invoke (http://localhost:8100/lib/ionic/js/ionic.bundle.js:17995:19)
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:17794:37
at getService (http://localhost:8100/lib/ionic/js/ionic.bundle.js:17941:39)
at injectionArgs (http://localhost:8100/lib/ionic/js/ionic.bundle.js:17965:58)
at Object.instantiate (http://localhost:8100/lib/ionic/js/ionic.bundle.js:18007:18) <ion-nav-view name="tab-search" class="view-container tab-content" nav-view="active" nav-view-transition="android">


This is my code so far:

search.controller.js

/*
*
*/
(function() {
'use strict';

angular
.module('dingocv.controllers')
.controller('SearchController', SearchController)


function SearchController($scope, $interval, CategoryService, MonitorService) {
$scope.connected = $interval(MonitorService, 1000);

CategoryService.getCategoryList().then(function(dataResponse) {
$scope.categories = dataResponse.data;
});
}

})();


connection.service.js

/*
*
*/
(function() {
'use strict';

angular
.module('dingocv.services')
.service('ConnectionService', ConnectionService)
.service('MonitorService', MonitorService)


function ConnectionService($rootScope, $cordovaNetwork) {
return {
isOnline: function() {
if(ionic.Platform.isWebView()) {
return $cordovaNetwork.isOnline();
} else {
return navigator.OnLine;
}
},
isOffline: function() {
if(ionic.Platform.isWebView()) {
return !$cordovaNetwork.isOnline();
} else {
return !navigator.OnLine;
}
}
}
}

function MonitorService() {
if(ConnectionService.isOnline()) {
return true;
} else if(ConnectionService.isOffline()) {
return false;
}
}

})();


search.view.html

<ion-view view-title="Search">
<ion-content>
<div ng-show="!connected">You are not connected</div>
<div ng-show="connected">You are connected</div>
</ion-content>
</ion-view>

Answer

I guess you are missing the dependency injection in connection.service.js

connection.service.js (function() { 'use strict';

    angular
    .module('dingocv.services')
    .service('ConnectionService', ConnectionService)
    .service('MonitorService', MonitorService)


    function ConnectionService($rootScope, $cordovaNetwork) {
        return {
            isOnline: function() {
                if(ionic.Platform.isWebView()) {
                    return $cordovaNetwork.isOnline();
                } else {
                    return navigator.OnLine;
                }
            },
            isOffline: function() {
          if(ionic.Platform.isWebView()) {
            return !$cordovaNetwork.isOnline();
          } else {
            return !navigator.OnLine;
          }
            }
        }
    }
    // injecting ConnectionService in this service
    function MonitorService(ConnectionService) {
        if(ConnectionService.isOnline()) {
            this.status = true;
        } else if(ConnectionService.isOffline()) {
            this.status = false;
        }
    }

})();

search.controller.js

(function() {
    'use strict';

    angular
    .module('dingocv.controllers')
    .controller('SearchController', SearchController)


    function SearchController($scope, $interval, CategoryService, MonitorService) {
        $interval(function(){
            $scope.connected = MonitorService.status
        }, 1000);

        CategoryService.getCategoryList().then(function(dataResponse) {
            $scope.categories = dataResponse.data;
        });
    }

})();

Hope it helps.