2017-06-07 17 views
0

リクエストと応答を記録するための単純なhttpインターセプタを作成しました。問題は、最初に実行されるconfigブロックでは未定義であり、どのモジュールがインスタンス化できなかったかによってエラーをスローします。以下は使用されるコードです:角度:httpインターセプタが設定ブロックに注入されていません

authApp 
.factory("httpInterceptor",function($log){ 
    var httpInterceptor = {}; 

    httpInterceptor.request = function(config){ 
     $log.info("request is being made to " + config.url); 
     return config; 
    }; 

    httpInterceptor.response = function(config){ 
     $log.info("request to URL : " + config.url + " is completed 
     now."); 
     return config; 
    }; 

    return httpInterceptor; 
}) 
.config(function($httpProvider){ 
    // Http Interceptor 
    $httpProvider.interceptors.push(httpInterceptor); 
}); 

ありがとうございます!

答えて

1

あなたのコードは、新しいインターセプタを$httpProviderのインターセプタのリストにプッシュしている特定の行を除いて正しいようです。

$httpProvider.interceptors.push(httpInterceptor); 

この行は、それが角度の工場のように定義されているためhttpInterceptorが何であるかを知りません。

$httpProvider.interceptors.push('httpInterceptor'); // Note the quotes around it. 

に変更して再試行してください。

+0

ありがとうございました...全体的な流れが同様に今も理にかなっています! –

関連する問題