TypeError: ConnectionService.isOnline不是一个函数

TypeError: ConnectionService.isOnline is not a function

本文关键字:一个 函数 ConnectionService isOnline TypeError      更新时间:2023-09-26

当我尝试在控制器中实例化我的服务时,我保持以下错误消息:

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">

这是我到目前为止的代码:

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>

我猜你错过了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;
        });
    }
})();

希望能有所帮助。