3

I need to set default header in $http service depending on current route. This is the code:

$http.defaults.headers.common.HeaderName = 'HeaderValue';

I need to watch current $location.path and change value of the header according the route.

Can I put this code in $routeProvider section? Something like:

$routeProvider
    .when('/route', {
        templateUrl: 'book.html',
        controller: 'BookController',
        resolve: function() {
                     //set default header here
        }
    })

What is the proper way to do that?

6
  • inject $location to your controller Commented Mar 21, 2014 at 15:53
  • but how can I set default header in controller? Commented Mar 21, 2014 at 15:56
  • are you asking how to get the route? $location.path() will be '/route' base on your example. set the header like your example. Commented Mar 21, 2014 at 16:03
  • 2
    I read your question a few more times very carefully, my understanding is you need to set http headers base on your current route. that means when you change route, you need a different headers. So why do you need to set headers in default $httpProvider. all services in angular is singleton. Commented Mar 21, 2014 at 16:17
  • 1
    You have understood my question correctly. I need to all my $http requests contain header, depended on route. I can add {headers: {'headerName': 'headerValue'} to each request manually. But I'm looking for the way to do it once. Please, explain the solution you offered (based on fact that services are singletons)? Commented Mar 21, 2014 at 16:25

2 Answers 2

3

$routeProvider is a provider, but on the other hand $http is an instance, so basically you cannot inject an instace to config block where you set $routeProvider, but you can inject $http instance to run block which suits you most...

ok let's deal the problem you face, actually you can do it any controller you want but you want it to do when only location/route changes so let's inject another instance $rootScope to run block to watch location/route changes...

but you have one more request set $http.defaults depends on $location.path() so let's inject another instance $location to run block to get current path...

so here is our final run block

app.run(function($rootScope, $http, $location) {
  $rootScope.$on('$locationChangeSuccess', function(event, next, current) {
    $http.defaults.headers.common.HeaderName = $location.path();
    console.log("Headers :",$http.defaults.headers);
  });
});

from now on after every location change trigger our watch where we set $http.headers depends on $location.path()...

here is working PLUNKER

UPDATE

if you want to look other $route and $location events check this PLUNKER

Sign up to request clarification or add additional context in comments.

Comments

0

You could change the header based on location by listening to the $routeChangeStart event, and changing the defaults there:

$rootScope.$on( '$routeChangeStart', function() {
    $http.defaults.headers.common.headerName = 'Path is ' + $location.path();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.