Javascript AngularJS-等待异步调用

Javascript AngularJS-等待异步调用,javascript,angularjs,Javascript,Angularjs,我开始学习AngularJS,到目前为止还不错,但我目前还停留在一个关于同步函数的小问题上 我希望在执行其余代码之前调用并返回函数AuthenticationSharedService.login()。我该怎么做 app.js myApp.config(['$routeProvider', '$httpProvider', 'USER_ROLES', function ($routeProvider, $httpProvider, USER_ROLES) { $route

我开始学习AngularJS,到目前为止还不错,但我目前还停留在一个关于同步函数的小问题上

我希望在执行其余代码之前调用并返回函数
AuthenticationSharedService.login()
。我该怎么做

app.js

myApp.config(['$routeProvider', '$httpProvider', 'USER_ROLES',
    function ($routeProvider, $httpProvider, USER_ROLES) {
        $routeProvider
            .when('/login', {
                templateUrl: 'views/login.html',
                controller: 'LoginController',
                access: {
                    authorizedRoles: [USER_ROLES.all]
                }
            })
            .when('/error', {
                templateUrl: 'views/error.html',
                access: {
                    authorizedRoles: [USER_ROLES.all]
                }
            })
            .otherwise({
                templateUrl: 'views/main.html',
                controller: 'MainController',
                access: {
                    authorizedRoles: [USER_ROLES.user]
                }
            });
    }])
    .run(['$rootScope', '$location', 'AuthenticationSharedService', 'Session', 'USER_ROLES',
        function($rootScope, $location, AuthenticationSharedService, Session, USER_ROLES) {
            $rootScope.$on('$routeChangeStart', function (event, next) {

                <!-- Bit of code to execute before going further -->
                if (!Session.login) {
                    console.log('First attempt to login');
                    AuthenticationSharedService.login();
                }
                <!-- /////////////////////////////////////////// -->

                if (AuthenticationSharedService.isAuthenticated()) {
                    // user is not allowed
                    $rootScope.$broadcast("event:auth-notAuthorized");
                } else {
                    // user is not logged in
                    $rootScope.$broadcast("event:auth-loginRequired");
                }
            });
        }]);
myApp.factory('AuthenticationSharedService', ['$rootScope', '$http', '$cookieStore', 'authService', 'Session', 'Account',
    function ($rootScope, $http, $cookieStore, authService, Session, Account) {
        return {
            login: function () {
                return Account.get(function(data) {
                    Session.create(data.login, data.roles);
                    authService.loginConfirmed(data);
                });
            }
        };
    }]);

您需要使用resolve。请参阅$routeProvider或-第二个链接非常好

Resolve是应注入控制器的依赖关系的映射。如果任何映射对象是函数,则将对函数进行求值并注入其返回值。如果函数返回承诺,则在解析承诺之前不会呈现视图

仅供参考,如果承诺被拒绝,该观点将根本不会被提出。您可能希望在$rootScope.$on('routeChangeError',functionToHandlerRejection)中处理这种情况

以下是一个适合您的示例:

.when('someUrl', {
  resolve: {
    object: function(AuthenticationSharedService) {
      return AuthenticationSharedService.login();
    }
  }
})

决定这这就是我想要的“如果一个函数返回一个承诺,那么在这个承诺得到解决之前,视图不会被呈现。”