2016-03-31 14 views
0

Amacım, kimlik doğrulaması veya arka uç kullanılabilirliği ile ilgili sorunları yakalamak için genel bir HTTP hata işleyicisi oluşturmaktır. Kodum benziyor aşağıdadır:AngularJS'de bir genel hata işleyicisi kaydediliyor

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'); 
    //... 
}) 

Ben şu istisna olsun bu çalıştırmayı denediğinizde (Diğer kodunu ve bağımlılıklar bu soru için sıyrılır): Eğer ben de görebileceğiniz gibi

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 
[email protected]://localhost/js/angular.js:1958:38 
[email protected]://localhost/js/angular.js:2032:14 
loadModules/<@http://localhost/js/angular.js:4524:22 
[email protected]://localhost/js/angular.js:321:11 
[email protected]://localhost/js/angular.js:4508:5 
loadModules/<@http://localhost/js/angular.js:4525:40 
[email protected]://localhost/js/angular.js:321:11 
[email protected]://localhost/js/angular.js:45 

AngularJS 1.5.2 kullanarak m.

Bu kodla ilgili sorun nedir?

+0

çalışacak umut http://jsfiddle.net/ Tam kodunuzdan jsalaat/LvhRC/90 /). Sadece iyi çalışıyor. –

+0

Sorun nedir? Yanıtları başarılı bir şekilde denedikten ve kodumu orijinal sürüme geri döndükten sonra da benim için çalışıyor. Belki de önbelleklemede bir sorun vardı ... (Yemin ederim bu tuzağı kontrol ettim) Sanırım bu soruyu sileceğim. Fakat çabalarınız için teşekkür ederim ve gürültü için özür dilerim. – Noir

+0

Endişelenmeyin. Bazen olur. –

cevap

-1

Sen modülüne $httpProvider geçmek zorunda: angular.module(function($httpProvider) {...}); veya

+0

'$ httpProvider' bir modül değildir. Yapılandırma aşamasında '$ http' için bir sağlayıcı. [$ httpProvider Docs] (https://docs.angularjs.org/api/ng/provider/$httpProvider) –

0
foo.config(["$httpProvider", function ($httpProvider) { 
    $httpProvider.interceptors.push("HttpErrorInterceptor"); 
}]); 

var foo= angular.module('foo',[$httpProvider]); bu deneyin, (ben bir [keman] oluşturduk