如何使用cookiesProvider在角度配置 [英] how to use cookiesProvider in angular config
问题描述
我要得到cookie值,并设置为供应商。这篇文章 http://stackoverflow.com/a/20415679/772481 提到的$ cookiesProvider。但我怎么使用它?
mod.config([someProvider,$ cookiesProvider功能(someProvider,$ cookiesProvider){
someProvider.set('CONFIGS',{'令牌':$ cookiesProvider [XSRF-TOKEN]})
}]);
我想对每个HTTP请求设置特定的HTTP头,所以这是我的解决方案:
我使用的运行功能,因为在配置我无法访问饼干,请参见的http:// docs.angularjs.org/guide/module
app.run(函数运行($ HTTP,$饼干){
$ http.defaults.headers.common [X-AUTH-TOKEN] = $饼干['AUTH-TOKEN'];
});
如果你不想使用运行功能该配置(因为它很难单元测试),你可以写为$拦截httpProvider,与此类似:的 https://gist.github.com/lpsBetty/76df8d1f037db87f4a0b
I want get cookie value and set to a provider. This post http://stackoverflow.com/a/20415679/772481 mentioned $cookiesProvider. But how do I use it?
mod.config(["someProvider", "$cookiesProvider", function(someProvider, $cookiesProvider) {
someProvider.set('configs', {'token': $cookiesProvider["XSRF-TOKEN"]})
}]);
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
这篇关于如何使用cookiesProvider在角度配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!