我建议您为此使用事件。诀窍是假设您的后端让请求失败并发送正确的拒绝访问状态(403),以观察不成功的服务调用。标志$rootScope.isLoggingIn 是为了防止在链中存在多个调用时出现多个LoginDialogs。
未经测试的代码是我的想法,所以请测试它并在出现错误/拼写错误时更正
<html ng-app="myApp">
</html>
angular.module('myApp', ['ui.bootstrap.modal'])
.run(function($rootScope, $modal) {
$rootScope.isLoggingIn = false;
$rootScope.$on('showLoginDialogEvent', function (e, evtArgs) {
$rootScope.isLoggingIn = true;
$modal.open({
templateUrl: 'views/loginView.html',
controller: 'LoginDialogController'})
.result.then(function(){
$rootScope.isLoggingIn = false;
});
}
});
angular.module('requestInterceptor', [])
.config(function ($httpProvider) {
$httpProvider.interceptors.push('RequestInterceptor');
})
.factory('RequestInterceptor', function ($q, $rootScope) {
return {
'request': function (config) {
return config || $q.when(config);
},
'requestError': function(rejection) {
return $q.reject(rejection);
},
'response': function(response) {
return response || $q.when(response);
},
'responseError': function(rejection) {
if(!$rootScope.isLoggingIn && rejection.status === 403)
$rootScope.$broadcast('showLoginDialogEvent');
return $q.reject(rejection);
}
}
}
角度 >= 1.2.0
角度-ui.bootstrap >= 0.7