Angularjs 添加自定义标题值

Angularjs 添加自定义标题值,angularjs,Angularjs,我对angularjs是新手。 目前正在学习拦截器 我需要的是,我想在头中添加一个新的头参数,如“isUserUpdated”:true,这样我就可以在服务器端识别用户是否已更新 我的代码如下: .factory('myInterceptor', [function(){ var myInterceptor = { request: function(config){ if(config.url.in

我对angularjs是新手。 目前正在学习拦截器

我需要的是,我想在头中添加一个新的头参数,如“isUserUpdated”:true,这样我就可以在服务器端识别用户是否已更新

我的代码如下:

.factory('myInterceptor', [function(){
            var myInterceptor = {
                request: function(config){
                    if(config.url.indexOf('/session') > -1) {
                        //add a new value in header
                    }
                    return config;
                },
            };
            return myInterceptor;
        }]);

是否有任何方法可以在标头中添加新的自定义键值对?

您需要为此创建一个http侦听器

var app = angular.module('app'); // get your app.
app.config([ '$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push('MyCustomInterceptor');
} ]);

app.service('MyCustomInterceptor', [ '$rootScope', function($rootScope) {
    var service = this;
    service.request = function(config) {
        config.headers.isUserUpdated = $rootScope.myValue; // true or false or whatever 
        return config;
    };
} ]);
config.headers.isUserUpdated将添加到应用程序完成的每个http调用中