How and where to define angular $http defaults?

364 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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) {
    ...
});
0
On

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.