为根范围承诺制作 angular 服务

making an angular service for a rootscope promise

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

ionic.utils 模块已正确注入我的 angular 应用程序。这个模块来自我的 services.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;
        }
    }
}]);

在我的 controllers.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");
    });
}])

但是,此控制器 returns 在状态转换时出现以下控制台错误:

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,它将成功函数作为第一个参数,将错误函数作为第二个参数。

success and error are functions on a promise that AngularJS adds for us when using $http or $resource. They're not standard, you won't find them on other promises.

代码

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

Return 从 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;
            }
        }
    }]);

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