2017-04-18 217 views
1

我試圖讓每個向服務器發出請求之前添加一個標記作爲http標頭的HTTP攔截器。

app.factory('httpRequestInterceptor', 
    ['$rootScope', function($rootScope) 
    { 
     return { 
      request: function($config) { 
       if($rootScope.token) 
       { 
        $config.headers['auth-token'] = $rootScope.token; 
       } 
       return $config; 
      } 
     }; 
    }]); 

上面是我的攔截器,它對我來說看起來沒問題。然後,我在配置狀態期間將此攔截器推送到http提供程序,如下所示。

app.config(function ($routeProvider, $httpProvider) { 
    $routeProvider 
     .when(...) 
     .otherwise({ 
      redirectTo: '/' 
     }); 
    $httpProvider.interceptors.push('httpRequestInterceptor'); 
}); 

現在看看我的瀏覽器控制檯日誌時,我得到一個錯誤說: "Unknown provider: httpRequestInterceptorProvider <- httpRequestInterceptor <- $http <- defaultErrorMessageResolver"

看來它無法解析攔截httpRequestInterceptor的依賴。我定義錯了嗎?

謝謝,感謝任何幫助!

+0

'defaultErrorMessageResolver'似乎是[角自動驗證](HTTP的一部分:// jonsamwell.github.io/angular-auto-validate/)。你是否將'jcs-autoValidate'模塊作爲依賴包含在內? – Phil

+0

@Phil是的,我已經包含jcs-autoValidate作爲模塊依賴,並自動驗證工作正常。 我剛剛更新了代碼片段以顯示整個app.js.我希望這可以幫助,並感謝伸出援手。 –

回答

0

對不起,在我的端部的愚蠢的錯誤,我忘記包括工廠在我的index.html:/

相關問題