[英]Globally set custom HTTP header in AngularJS from ngInit
I am unable to set custom HTTP header taken from ngInit
in AngularJS. 我无法设置从AngularJS中的
ngInit
获取的自定义HTTP标头。
I set variable using ngInit in html: 我在html中使用ngInit设置了变量:
<div ng-init="myApiKey='valueOfVar'"></div>
Now I want to use this variable in all HTTP requests. 现在,我想在所有HTTP请求中使用此变量。 I tried to set it in app.config and in controller too.
我试图在app.config和控制器中设置它。
If I set it immediately, variable is undefined. 如果我立即设置,则变量未定义。
If I set it in $scope.$watch
callback function, variable is defined, but HTTP requests are without that header. 如果我在
$scope.$watch
回调函数中设置了变量,则定义了变量,但是HTTP请求没有该标头。
I set header with: 我设置标头与:
$http.defaults.headers.common.Authorization = 'something';
(or via $httpProvider
, when in app.config) and then use it in service utilizing $resource
. (或在app.config中时通过
$httpProvider
),然后通过$resource
在服务中使用它。 My code looks like [this][1] 我的代码看起来像[this] [1]
I tried to set it in config
, in run
, in controller and as a service, but nothing worked for me yet. 我试图在
config
, run
,controller和服务中设置它,但是对我来说什么都没有用。 Thank you in advance for any advice given. 预先感谢您提供任何建议。
Edit: Now I have interceptor: 编辑:现在我有拦截器:
myApp.config(function ($httpProvider) {
$httpProvider.interceptors.push('httpRequestInterceptor');
});
myApp.factory('httpRequestInterceptor', ['$rootScope', function($rootScope) {
return {
request: function($config) {
$config.headers['Authorization'] = 'Basic ' + $rootScope.apiKey;
return $config;
}
};
}]);
But $rootscope.apiKey
(which is set in main controller) is undefined
at first call. 但是
$rootscope.apiKey
(在主控制器中设置)在第一次调用时undefined
。 After that, it's okay (and set). 之后,就可以了(并设置)。
the following code will allow you to intercept all http requestes of your application and to add a property 'language' in the header of the request 以下代码将允许您拦截应用程序的所有http请求,并在请求的标头中添加属性“ language”
angular.module('app', ['ngRoute', 'ngResource']).run(function($http) {
$http.defaults.headers.common.language='fr-FR';
});
we can add many properties in the http header 我们可以在http标头中添加许多属性
If you wanna modify request/response in AngularJS you should do a deeply read about Interceptor : 如果您想在AngularJS中修改请求/响应,则应深入了解Interceptor :
You can call $http(config), where config is a object with every configuration. 您可以调用$ http(config),其中config是每个配置的对象。 There's a configuration there for
headers
, where you should put your code: headers
有一个配置,您应该在其中放置代码:
headers – {Object} – Map of strings or functions which return strings representing HTTP headers to send to the server.
标头– {Object} –字符串或函数的映射,这些字符串或函数返回表示HTTP标头的字符串以发送到服务器。 If the return value of a function is null, the header will not be sent.
如果函数的返回值为null,则不会发送标头。
If you google about Interceptor, you will get a full list of blogs explaining how to use It, and providing example codes, here's one that help me out sometime ago: 如果您搜索有关Interceptor的信息,则将获得完整的博客列表,其中解释了如何使用它,并提供了示例代码,这在一段时间之前对我有帮助:
OK, so for anybody trying to do the same - Interceptors were the key (thank you Fals a lot), but problem was with the $rootScope
. 好的,所以对于任何想做同样事情的人来说, 拦截器是关键(非常感谢Fals ),但是
$rootScope
问题是。 As I wrote in question, when Interceptor was called the first time, $rootScope.apiKey
wasn't set yet (on consecutive requests, it was already set). 正如我在写问题时所写的那样,当首次调用Interceptor时,尚未设置
$rootScope.apiKey
(在连续的请求中,它已经被设置)。
Right way (or The working way ) was to pass variable to AngularJS not through ngInit
, but through $window
. 正确的方式(或工作方式 )是不通过
ngInit
而是通过$window
将变量传递给AngularJS。 So I added to my HTML code: 所以我添加了我的HTML代码:
<script>window.apiKey = 'myValue';</script>
and then pass it to Interceptor exactly the same way as $rootScope
- You can literally just replace all occurences of $rootScope
in edit of my question with $window
and now it's working - even for the first request! 然后将其传递给Interceptor,方法与
$rootScope
完全相同-您可以用$window
替换我编辑问题时出现的$rootScope
所有出现,并且现在可以使用-即使是第一个请求!
Thank you Fals once again. 再次感谢您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.