5

I am considering to add a default header for the $http service, which value is an access token that will be generated after user authentification.

module.config('$routeProvider', '$locationProvider', '$httpProvider'){
    $httpProvider.defaults.headers.post['XSRF-AUTH'] = 
        "some accessToken to be generated later"; 
}

Problem is, the config() block is applied when Angular bootstraps its core components. Is there a way to alter $ĥttpProvider dynamically ?

4

1 に答える 1

16

$httpProvider の代わりに、実行時に $http オブジェクトを介してデフォルトのヘッダーを変更できます。たとえば、構成ブロックの外で次のことができます。

$http.defaults.headers.post['XSRF-AUTH'] = "access token";

詳細については、$http api ドキュメントを確認してくださいhttp://docs.angularjs.org/api/ng/service/$http#setting-http-headers

于 2014-04-17T03:31:45.090 に答える