为诺贝尔承诺提供有针对性的服务

时间:2015-01-23 17:47:14

标签: javascript angularjs angular-promise angular-services angularjs-http

我正在尝试确保在加载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

2 个答案:

答案 0 :(得分:2)

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

的大量依赖项
.controller('FrameCtrl', [ 'Rootscope', function($scope, $state, 
$rootScope, Rootscope) {

应该是

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

对我来说,命名服务Rootscope确实让我感到困惑!

答案 1 :(得分:1)

通常使用promises我们只使用.then,它将success函数作为第一个参数,将error函数作为第二个参数。

  

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

希望这会对你有所帮助。感谢。