我遇到了这个问题,我需要在应用程序引导之前对用户进行身份验证。我尝试过以下但是问题是模块$ routeChangeStart事件在我手动调用bootstrap方法之前以某种方式被触发。
window.onload = function (){
var $rootElement, modules, $injector, $compile, compositeLinkFn, $rootScope, $localStorage, Security, SecurityRepository;
// without this I'll get Uncaught Error: [$injector:unpr] Unknown provider: $rootElementProvider <- $rootElement <- $location <- $location
$rootElement = angular.element(window.document.body);
modules = [
'ng', // angular module
'myapp.security', // the only module I need before bootstrapping
function ($provide) {
$provide.value('$rootElement', $rootElement);
}
];
// one injector per application
$injector = angular.injector(modules);
// Compile Service: it traverses the DOM and look for directives and compile and return linking function. No accecess to scope
$compile = $injector.get('$compile');
// Hold of the rootscope
$rootScope = $injector.get('$rootScope');
$localStorage = $injector.get('$localStorage');
if ($localStorage.token) {
Security = $injector.get('Security');
SecurityRepository = $injector.get('SecurityRepository');
refreshToken()
.then(getUser)
.then(setUser)
.then(function (response) {
// collection of all linking function. Here scope is getting accessed
compositeLinkFn = $compile($rootElement);
compositeLinkFn($rootScope);
$rootScope.$apply();
// If I do this, everything gets back to initial state
// angular.bootstrap(document.body, ['parisrioApp']);
});
} else {
// collection of all linking function. Here scope is getting accessed
compositeLinkFn = $compile($rootElement);
compositeLinkFn($rootScope);
$rootScope.$apply();
}
////////////////////////////////////////////////////////////////
function refreshToken() {
var oldToken;
oldToken = $localStorage.token;
return SecurityRepository.refreshToken(oldToken.refreshToken)
.then(refreshTokenComplete, refreshTokenFailed);
function refreshTokenComplete(response) {
$localStorage.token = response.data;
return $localStorage.token;
}
function refreshTokenFailed(error) {
delete $localStorage.token;
}
}
function getUser() {
return SecurityRepository.getUserInfo();
}
function setUser(response) {
Security.changeUser(response.data);
}
}
有没有办法解决这个问题?
我将上面的代码放在最后一个脚本块中(即在角度和每个与应用程序相关的js文件之后)。运行的版本是Angular 1.2.28