为根范围承诺提供角度服务

making an angular service for a rootscope promise

本文关键字:服务 范围 承诺      更新时间:2023-09-26

我试图确保在加载frame状态时,我的$rootScope具有从以前的状态定义的所有必要属性。

ionic.utils模块已正确注入我的角度应用程序中。此模块来自我的服务.js文件。

angular.module('ionic.utils', [])
.factory('dataService', ['$rootScope','$q','$timeout', function($rootScope, $q, $timeout) {
    return {
        get: function() {
            var deferred = $q.defer();
            $timeout(function() {
              deferred.resolve($rootScope);
            }, 2000);
            return deferred.promise;
        }
    }
}]);

在我的控制器.js文件中,这是我frame状态的相应控制器:

.controller('FrameCtrl', ['$scope','$state','$rootScope','dataService',
function($scope, $state, $rootScope, dataService) {
    // get active address and delivery time.
    dataService.get().success(function() {
        console.log("derp");
    });
}])

但是,此控制器在状态转换时返回以下控制台错误:

ionic.bundle.js:17696 TypeError: Cannot read property 'get' of undefined
    at new <anonymous> (controllers.js:201)
    at invoke (ionic.bundle.js:11591)
    at Object.instantiate (ionic.bundle.js:11602)
    at $get (ionic.bundle.js:14906)
    at updateView (ionic.bundle.js:42986)
    at IonicModule.directive.directive.compile.eventHook (ionic.bundle.js:42933)
    at Scope.$get.Scope.$broadcast (ionic.bundle.js:20605)
    at $state.transitionTo.$state.transition.resolved.then.$state.transition (ionic.bundle.js:34122)
    at deferred.promise.then.wrappedCallback (ionic.bundle.js:19197)
    at ionic.bundle.js:19283

我在编写的服务中找不到错误。一些帮助将不胜感激!

编辑将依赖注入添加到我的控制器后,现在错误已更改。在这里:

TypeError: object is not a function
    at new <anonymous> (controllers.js:202)
    at invoke (ionic.bundle.js:11591)
    at Object.instantiate (ionic.bundle.js:11602)
    at $get (ionic.bundle.js:14906)
    at updateView (ionic.bundle.js:42986)
    at IonicModule.directive.directive.compile.eventHook (ionic.bundle.js:42933)
    at Scope.$get.Scope.$broadcast (ionic.bundle.js:20605)
    at $state.transitionTo.$state.transition.resolved.then.$state.transition (ionic.bundle.js:34122)
    at deferred.promise.then.wrappedCallback (ionic.bundle.js:19197)
    at ionic.bundle.js:19283

控制器中的依赖项数组缺少传递给参数的大量依赖项

.controller('FrameCtrl', [ 'Rootscope', function($scope, $state, 
$rootScope, Rootscope) {

应该是

.controller('FrameCtrl', ['$scope','$state', '$rootScope', 'Rootscope', function($scope, $state, 
    $rootScope, Rootscope) {

当然,将服务命名为Rootscope似乎让我感到困惑!

通常对于 promise,我们只使用 .then ,它将成功函数作为第一个参数,将错误函数作为第二个参数。

successerror是AngularJS添加的承诺的函数 为我们使用$http或$resource。它们不是标准的,你 不会在其他承诺中找到他们。

法典

dataService.get().then(function() { 
    console.log("derp"); 
});

返回deferred.resolve()不见了

 angular.module('ionic.utils', []).factory('dataService', ['$rootScope', '$q', '$timeout', function($rootScope, $q, $timeout) {
        return {
            get: function() {
                var deferred = $q.defer();
                $timeout(function() {
                    return deferred.resolve($rootScope);
                }, 2000);
                return deferred.promise;
            }
        }
    }]);

希望这对您有所帮助。谢谢。