在AngularJS中注册全局错误处理程序失败

时间:2016-03-31 10:16:41

标签: javascript angularjs

我的目标是创建一个全局HTTP错误处理程序来捕获后端身份验证或可用性方面的问题。我的代码如下所示:

var foo= angular.module('foo',[]);

foo.factory('HttpErrorInterceptor',function($q){
    return {
        'response': function(response) {
            return response || $q.when(response);
        },

        'responseError': function(rejection) {
            console.log(rejection);
            return $q.reject(rejection);
        }
    };
});

foo.config(function($httpProvider){
    $httpProvider.interceptors.push('HttpErrorInterceptor');
    //...
})

当我尝试运行此问题时(此问题会删除其他代码和依赖项)我得到以下异常:

Error: [$injector:modulerr] Failed to instantiate module foo due to:
[$injector:modulerr] Failed to instantiate module $httpProvider due to:
[$injector:nomod] Module '$httpProvider' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.5.2/$injector/nomod?p0=%24httpProvider
minErr/<@http://localhost/js/angular.js:68:12
module/<@http://localhost/js/angular.js:2034:17
ensure@http://localhost/js/angular.js:1958:38
module@http://localhost/js/angular.js:2032:14
loadModules/<@http://localhost/js/angular.js:4524:22
forEach@http://localhost/js/angular.js:321:11
loadModules@http://localhost/js/angular.js:4508:5
loadModules/<@http://localhost/js/angular.js:4525:40
forEach@http://localhost/js/angular.js:321:11
loadModules@http://localhost/js/angular.js:45

正如您所看到的,我使用的是AngularJS 1.5.2。

此代码有什么问题?

2 个答案:

答案 0 :(得分:0)

if __name__ == "__main__":
    Test().main()

试试这个,希望它能起作用

答案 1 :(得分:-1)

您必须将$httpProvider传递给模块:angular.module(function($httpProvider) {...});var foo= angular.module('foo',[$httpProvider]);