1

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

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

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

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
4

2 回答 2

2

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

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

应该

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

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

于 2015-01-23T18:00:12.197 回答
1

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

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

代码

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

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

于 2015-01-23T21:20:39.860 回答