我的配置文件如下所示:
app.config(['$httpProvider', '$locationProvider', '$sceProvider', '$state', '$stateProvider', function (
$httpProvider, $locationProvider, $sceProvider, $state, $stateProvider) {
$sceProvider.enabled(false);
$locationProvider.html5Mode(true);
$httpProvider.interceptors.push(authInterceptor);
var authentication = {
name: 'authentication',
url: '/authentication',
views: {
'root': {
templateUrl: function (stateParams) {
return '/Content/app/authentication/partials/home.html';
}
}
}
};
$stateProvider
.state(authentication)
.etc etc etc
这是我的拦截器:
app.factory('authInterceptor', function ($q, $rootScope, $state) {
function success(response) {
return response;
}
function error(response) {
if (response.status === 401) {
$state.transitionTo('authentication')
return $q.reject(response);
}
else {
return $q.reject(response);
}
}
return function (promise) {
return promise.then(success, error);
}
})
我遇到的问题是,当我运行应用程序时,我收到一条消息:
Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:unpr] Unknown provider: $state
如果我收到401,可以有人就如何改变状态给我一些建议吗?
答案 0 :(得分:1)
如果您使用了事件系统并$rootScope
?
例如,在您的拦截器中:
$rootScope.$broadcast('authenticationFailed');
在某些家长控制器中:
$rootScope.$on('authenticationFailed', function(event){
// authentication failed, so transition or what ever
$state.transitionTo('authentication');
});