I want to add some headers before calling $http's get method. I tried this:
$http = $originalHttpMethod;
this.$http = function() {
//add headers
return $http
};
$http.get({url:'http://...'});
but i get this error:
TypeError: Object function () {return $http} has no method 'get'.
I want to do this without crash original class.
How can i done this ?
Make a service instead of doing anything local. This way you can reuse it much easier across your application.
module.factory('$myHttp', ['$http', function($http) {
//do your stuff
return $http;
}]);
And then inside your controllers, directives or any other services, just include it as a dependency and use it like so:
$myHttp.get()
Here's some more info about that:
You can define your headers in the config parameter object when you call the $http service:
configObj = {url:'http://...', headers: { add your headers here}, ... };
$http.get(configObj);