Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get headers when ng-view changes

All of our content pages have a particular header, X-Foo. When the content of the ng-view changes, we want to display the new page's X-Foo header in a different element. How can we get this value whenever the content changes?

EDIT: Since this was apparently unclear, the header is expected in the response, not the request.

like image 739
user1207177 Avatar asked Mar 02 '17 16:03

user1207177


1 Answers

You can use a httpInterceptor for this. HTTP interceptors are a great way to define behavior in a single place for how a request or response is handled for all HTTP calls using the $http service

app.config(function ($httpProvider) {
    $httpProvider.interceptors.push('httpInterceptor');
}).factory('httpInterceptor', function (liveInterviewConfiguration) {
    return {
        request : function (request) {
            console.info("Your request headers are");
            console.info(request.headers);
            return request;
        },
        requestError : function (error) {
            return error;
        },
        response : function (response) {
            return response;
        },
        responseError : function (error) {
            return error;
        }
    };
});
like image 177
Pramod_Para Avatar answered Nov 15 '22 00:11

Pramod_Para