使用全局错误处理程序在 AngularJS 中显示错误消息
Show errormessage in AngularJS using a global errorhandler
我正在创建一个 angular JS 使用全局错误处理程序来捕获我所有的 ajax 错误。
使用 JQuery,我只是将 div 附加到我的正文顶部,并附上错误内容。
在 Angular 中,我找不到以正确方式执行此操作的方法。
使用 $scope 给我一个模块错误。
这是我正在使用的代码(在 Whosebug 上找到它):
(function(){
var httpInterceptor = function ($provide, $httpProvider) {
$provide.factory('httpInterceptor', function ($q, $location) {
return {
response: function (response) {
return response || $q.when(response);
},
responseError: function (rejection) {
if(rejection.status === 401) {
//show message not autorized
}
if(rejection.status === 500) {
$location.path("/error");
}
return $q.reject(rejection);
}
};
});
$httpProvider.interceptors.push('httpInterceptor');
};
angular.module("testApp").config(httpInterceptor);
}());
当遇到 500 错误时,我会重定向到错误页面,这很好,但是当我遇到不太严重的错误时,我只想将它们显示在我的内容上方,如何在不使用 JQuery 的情况下实现这一点并使用完整的 Angular 代码?
我最终使用了我在此处找到的即时消息插件:http://sachinchoolur.github.io/angular-flash/
非常轻巧,效果很好。
我正在创建一个 angular JS 使用全局错误处理程序来捕获我所有的 ajax 错误。 使用 JQuery,我只是将 div 附加到我的正文顶部,并附上错误内容。 在 Angular 中,我找不到以正确方式执行此操作的方法。 使用 $scope 给我一个模块错误。 这是我正在使用的代码(在 Whosebug 上找到它):
(function(){
var httpInterceptor = function ($provide, $httpProvider) {
$provide.factory('httpInterceptor', function ($q, $location) {
return {
response: function (response) {
return response || $q.when(response);
},
responseError: function (rejection) {
if(rejection.status === 401) {
//show message not autorized
}
if(rejection.status === 500) {
$location.path("/error");
}
return $q.reject(rejection);
}
};
});
$httpProvider.interceptors.push('httpInterceptor');
};
angular.module("testApp").config(httpInterceptor);
}());
当遇到 500 错误时,我会重定向到错误页面,这很好,但是当我遇到不太严重的错误时,我只想将它们显示在我的内容上方,如何在不使用 JQuery 的情况下实现这一点并使用完整的 Angular 代码?
我最终使用了我在此处找到的即时消息插件:http://sachinchoolur.github.io/angular-flash/
非常轻巧,效果很好。