When I try to create an instance of my service in the controller, I save the following error message:
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:
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>
javascript angularjs
methuselah
source share