Miguel Moura
Miguel Moura

Reputation: 39364

How and where to define angular $http defaults?

I am trying to set the defaults values of $http on an angular application so I have:

var application = angular.module('Application', ['ngDialog', 'validation']).config(function ($http) {

  $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

});

This originates an error. How and where can I set the defaults of $http?

Upvotes: 3

Views: 366

Answers (2)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

It should be $httpProvider instead of $http only

At angular configuration time provider will be accesible as suffix with 'Provider'

Its preferable to set providers setting in configuration phase of angular rather than run phase.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691635

The config phase is used to configure the service providers. Once the config phase is over, the run phase starts, where the providers are used to create the services.

The corollary is that during the config phase, you can't access any service. You can do that during the run phase though:

application.run(function($http) {
    ...
});

Note that the $httpProvider also allows configuring defaults, so you could also do

application.config(function($httpProvider) {
    ...
});

Upvotes: 4

Related Questions