我有一个拦截器:
angular.module('mobileDashboardApp')
.factory('HTTPInterceptor', function ($rootScope, $q, HTTPErrors) {
return {
responseError: function (response) {
$rootScope.$broadcast({
500: HTTPErrors.serverError,
503: HTTPErrors.serviceError
}[response.status], response);
return $q.reject(response);
}
};
})常量的定义如下:
angular.module('mobileDashboardApp')
.constant('HTTPErrors', {
serverError: 'internal-server-error',
serviceError: 'service-error'
})当出现500时错误时,我如何运行$rootScope.$on来执行console.log?
在我的配置中,我使用以下命令添加了拦截器:
.config(function ($httpProvider, $routeProvider) {
$httpProvider.interceptors.push([
'$injector',
function ($injector) {
return $injector.get('HTTPInterceptor');
}
]);发布于 2016-01-07 04:09:53
据我所知,您正在广播一个关于500状态的HTTPErrors.serverError字符串事件和一个关于503状态的HTTPErrors.serviceError字符串事件。因此,您可以通过在想要订阅这些事件的任何控制器上使用此代码来捕获它们
$rootScope.$on(HTTPErrors.serverError, function(event, args){
console.log("500");
});
$rootScope.$on(HTTPErrors.serviceError, function(event, args){
console.log("503");
});当然,您需要将HTTPErrors注入到控制器中。
https://stackoverflow.com/questions/34641528
复制相似问题