你可以做这样的事情
.when('/viewprojects', {
templateUrl: '/views/projects.html',
controller: 'ProjectViewController',
resolve: {
authenticate: authenticateCb
}
})
其中authenticateCb
是:
/**
* A callback to authenticate routes navigation.
* @param {Object} Authenticator Authentication service
* @returns {Object} authenticated promise.
*/
var authenticateCb = function (AuthenticatorService) {
return AuthenticatorService.authenticated();
};
AuthenticatorService去如下:
function AuthenticatorService($q, $rootScope, $location, NotifierService, ViewPath) {
/**
* Authentics the user on route changes and navigate to corresponding
* view. If the user is not authenticated then it naviagtes the user
* to Log in page.
* @returns {Object} authenticated promise.
*/
this.authenticated = function() {
var deferred = $q.defer();
if ($rootScope.token) {
deferred.resolve('Authenticated.');
} else {
NotifierService.notify('PLEASE_LOGIN_AGAIN', 'error');
$location.path(ViewPath.LOGIN_PAGE);
deferred.reject();
}
return deferred.promise;
};
}
希望这有助于。