Angularjs 如何在全局错误处理中使用$http.post?
我有一个带有AngularJS 1.3单页应用程序组件的MVC5.0 web项目。我想在发生任何未处理的角度异常时发回MVC控制器,因此我如下所示:Angularjs 如何在全局错误处理中使用$http.post?,angularjs,angularjs-http,Angularjs,Angularjs Http,我有一个带有AngularJS 1.3单页应用程序组件的MVC5.0 web项目。我想在发生任何未处理的角度异常时发回MVC控制器,因此我如下所示: mod.factory('$exceptionHandler', ['$http', function($http) { return function(exception, cause) { console.log(exception.message); $http.post("Applicatio
mod.factory('$exceptionHandler', ['$http', function($http) {
return function(exception, cause) {
console.log(exception.message);
$http.post("Application/CatchAngularError", {
exception: exception,
cause: cause
});
}
}]);
在Chrome中,当我加载包含此脚本的页面时,Javascript控制台报告“未捕获错误:[$injector:cdep]$injector/cdep?p0=%24rootScope%20%3C-%20%24http%20%3C-%20%24exceptionHandler%20%3C-%20%24rootScope”。显然,$http已经对$exceptionHandler有依赖关系,所以我不能给$exceptionHandler一个对$http的依赖关系,因为这会创建一个循环依赖关系
我想我可以使用jQuery.ajax,但如果存在的话,我更喜欢角度更高的解决方案。有人有吗?您可以使用$injector服务并查找$http服务:
mod.factory('$exceptionHandler', ['$injector', function($injector) {
return function(exception, cause) {
console.log(exception.message);
var $http = $injector.get('$http');
$http.post("Application/CatchAngularError", {
exception: exception,
cause: cause
});
};
成功了。谢谢这是可行的,但在我的例子中,我不得不使用
exception.toString()
,因为exception
看起来不像一个简单的字符串(无论如何在AngularJS 1.5中)。