0

TL;DR;

I want to get all headers from the $http service and display them to the page. I don't care if it is only a list of the keys or the key/value pairs.

  • How can I do this?
  • Why has what I have been doing not working?

What I want to do

  $http
    .get( '/data/?some=parameters&go=here' )
    .success( function( response, status, headers ) {
      $scope.headers = headers();
    });

This outputs correctly in the Chrome console if I console.log either headers() or $scope.headers.

Only thing that I can do that works

  $http
    .get( '/data/?some=parameters&go=here' )
    .success( function( response, status, headers ) {
      $scope.headers['cache-control']     = headers('cache-control');
      $scope.headers['connection']        = headers('connection');
      $scope.headers['content-type']      = headers('content-type');
      $scope.headers['date']              = headers('date');
      $scope.headers['expires']           = headers('expires');
      $scope.headers['keep-alive']        = headers('keep-alive');
      $scope.headers['pragma']            = headers('pragma');
      $scope.headers['server']            = headers('server');
      $scope.headers['transfer-encoding'] = headers('transfer-encoding');
      $scope.headers['x-powered-by']      = headers('x-powered-by');
      // etc....
    });

This actually works in code, but I have to manually tell it every header to expect.

Meanwhile in my view

<div class="row" ng-repeat="(key,value) in headers">
  <div class="col-xs-4 text-right">{{ key }}</div>
  <div class="col-xs-8 text-left">{{ value }}</div>
</div>

AngularJS v1.3.15 was hurt during the making of this question.

1 Answer 1

3

I don't know why and how angular produces the headers object, but the reason of this error is that the object has no hasOwnProperty() function, which is used by ng-repeat loop to avoid standard JS functions calls.

What I managed to do is simple copying of the properties in a newly created object to make it work:

$scope.headers = {}

var tmp = headers()

for (var key in tmp) {
  $scope.headers[key] = tmp[key]
}

See the plunker as an example.

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

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.