ui-router冻结时添加$ http或restangular里面的解析

时间:2015-01-31 03:31:06

标签: angularjs angular-ui-router

所以我修复了ui-router / angular unpr问题!现在,我想知道为什么ui-router一旦我添加$ http或restangular调用就不会继续。也许我的延迟语法是错误的。这是代码

.state("main", {
    url: "/",
    views: {
        '' : { templateUrl: 'views/main.html' },
        'performance-ui-view@main': {
            templateUrl: 'views/gdreport.html',
            resolve: {
                propertyUserData : function(User, RestFactory, ApiEndpoints, messageService, $q) {
                    var deferred = $q.defer();
                    RestFactory.setBaseUrl(ApiEndpoints.DataService);
                    RestFactory.all('gdpermissions').get('access', { 'userresourceid' : userData.resourceId })
                        .then(function(response) {
                            $rootScope.userData.isGlobalAccount = true;
                            User.saveData();
                            deferred.resolve(response);
                            return deferred.promise;
                        }, function(err){
                            // f'ing work around, I've already told 401 to our backend dev
                            $rootScope.userData.isGlobalAccount = false;
                            userData.saveData();
                            deferred.resolve(response);
                        })
                }
            },
            controller : 'GlobalDashboardController'
        }
    }
})

1 个答案:

答案 0 :(得分:1)

不确定错误是什么(因为问题中没有提到),好像你没有按照你想要的方式从resolve函数返回一个promise。

  propertyUserData : function(User, RestFactory, ApiEndpoints, messageService, $q) {
        var deferred = $q.defer();
        RestFactory.setBaseUrl(ApiEndpoints.DataService);
        RestFactory.all('gdpermissions').get('access', { 'userresourceid' : userData.resourceId })
            .then(function(response) {
                $rootScope.userData.isGlobalAccount = true;
                User.saveData();
                deferred.resolve(response);

            }, function(response){
                // f'ing work around, I've already told 401 to our backend dev
                $rootScope.userData.isGlobalAccount = false;
                userData.saveData();
                deferred.resolve(response);
            });
      return deferred.promise; //<-- Here
    }

但是你可以放弃推迟创建的对象,因为你的api调用已经返回了一个promise,只需这样做:

  propertyUserData : function(User, RestFactory, ApiEndpoints, messageService, $q) {

        RestFactory.setBaseUrl(ApiEndpoints.DataService);
        return RestFactory.all('gdpermissions').get('access', { 'userresourceid' : userData.resourceId })
            .then(function(response) {
                $rootScope.userData.isGlobalAccount = true;
                User.saveData();
                return response;

            }, function(response){
                $rootScope.userData.isGlobalAccount = false;
                userData.saveData();
                return response;
            });

    }