使用全局errorhandler在AngularJS中显示errormessage
我正在使用全局错误处理程序创建一个angular JS,以捕获所有ajax错误。 使用JQuery,我只需在我的正文顶部添加一个div,其中包含errorcontent。 从角度来说,我找不到一个合适的方法来做这件事。 使用$scope会导致模块错误。 这是我正在使用的代码(在stackoverflow上找到):使用全局errorhandler在AngularJS中显示errormessage,angularjs,error-handling,Angularjs,Error Handling,我正在使用全局错误处理程序创建一个angular JS,以捕获所有ajax错误。 使用JQuery,我只需在我的正文顶部添加一个div,其中包含errorcontent。 从角度来说,我找不到一个合适的方法来做这件事。 使用$scope会导致模块错误。 这是我正在使用的代码(在stackoverflow上找到): 当收到500个错误时,我重定向到一个errorpage,这很好,但是当我遇到不太严重的错误时,我只想在我的内容上方显示它们,不使用JQuery和完整的Angle代码,我如何实现这一点呢
当收到500个错误时,我重定向到一个errorpage,这很好,但是当我遇到不太严重的错误时,我只想在我的内容上方显示它们,不使用JQuery和完整的Angle代码,我如何实现这一点呢?我最终使用了我在这里找到的flashmessage插件: 这是相当轻,工作非常好
(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);
}());