分配给promise后,页面没有加载Angularjs

时间:2015-02-09 21:27:23

标签: javascript angularjs

我正在尝试使用Angularjs创建登录脚本。我有一个身份验证服务来处理它的逻辑。

这里的服务:

    .factory("AuthSvc",
        ['$http','$q','$state',
          function ($http,$q,$state) {
            function GetUser() {
                var d = $q.defer();
                $http.get('api/auth/check')
                .success(function(res){
                d.resolve(res);
                });
                return d.promise;
            };
        return {
        login: function (credentials, form) {
             var defer = $q.defer();
             $http.post('/api/auth/login', credentials)
             .success(function (response) {
                form.$setPristine();
                user = response;
                $state.go('base.posts.content');
                defer.resolve();    
             })
             .error(function (response) {
                defer.reject();
             });
             return defer.promise;
        },

        isLoggedIn: GetUser,

        user: null 
};
        }]);

我有一个resolve属性和一个用户属性的监听器来更新应用配置的视图,如下所示:

.config(['$stateProvider', 
          function ($stateProvider) {                          
            // Now set up the states
            $stateProvider
                .state('base', {
                        url        : "",
                        templateUrl: "/templates/base/index.html",
                        resolve    : {user : ["AuthSvc",    
                            function(AuthSvc)  {
                                return AuthSvc.user;
                            }]
                    },
                })
                .state('base.login', {
                        url        : "login",
                        templateUrl: "/templates/login/index.html",
                        controller : 'LoginCtrl'
                })                    
                .state('base.posts', {   
                        abstract   : true,
                        url        : 'posts',
                        templateUrl:  '/templates/posts/posts.html'

               })
              .state('base.posts.content',{
                      url: '',
                      resolve: {    
                          posts: function (PostsSvc) {
                            return PostsSvc.getPosts();
                          }
                      },
                      views:{
                          'create': { 
                             templateUrl: '/templates/posts/create.html',
                             controller: 'PostsCtrl'
                      },
                        'list': {
                             templateUrl: '/templates/posts/list.html',
                             controller: 'PostsCtrl'
                        }
                     }    
            });

    }]);

    app.run(['$rootScope','AuthSvc','$state',
            function ($rootScope,AuthSvc,$state,
                ) {
                    $rootScope.$state = $state;
                    $rootScope.$stateParams = $stateParams;

                    $rootScope.$on('$stateChangeStart', 
                        function(event, next, current){                     
                            AuthSvc.user =  AuthSvc.isLoggedIn();
                        }); 

                    $state.transitionTo('base.posts.content');

                    }]);
}());

$rootScope.on('$stateChangeStart')无效。我看到一些代码,人们在不使用promise的then函数的情况下直接为promise分配值。如何使用此代码完成此操作?

0 个答案:

没有答案