how to use cookiesProvider in angular config how to use cookiesProvider in angular config angularjs angularjs

how to use cookiesProvider in angular config


You can inject $cookies manually:

myApp.config(function() {  var $cookies;  angular.injector(['ngCookies']).invoke(['$cookies', function(_$cookies_) {    $cookies = _$cookies_;  }]);  // here you can use $cookies as usual});


I wanted to set specific http headers on every http request, so this is my solution:

I'm using the run function because in config I couldn't access cookies, see http://docs.angularjs.org/guide/module

app.run(function run( $http, $cookies ){  $http.defaults.headers.common["X-AUTH-TOKEN"] = $cookies['AUTH-TOKEN'];});

If you don't want to use the run function for that configuration (because it's hard to unit-test), you can write an interceptor for the $httpProvider, similar to this: https://gist.github.com/lpsBetty/76df8d1f037db87f4a0b


Also you can write something like this:

$cookiesProvider.$get()["XSRF-TOKEN"]