How to use an angular service outside the angular structure?

1

At angular we have a service called $http . I know that to call it just pass as a parameter of a closure, that the angular makes the magic:

angular.module('foo').controller(function ($http) { 
   /** ... **/ 
});

What if I want to use $http out of controller or config ? How?

Is there a way to use an angular service other than by injecting dependencies into function parameters?

For example:

var $http = ...; // pego a instância do serviço aqui...

$http.get('/pagina-do-site').then(function () {

})
    
asked by anonymous 04.10.2016 / 19:27

2 answers

0
Assuming you want to use $http outside the angular internal structure, you use the angular.injector method:

var $http = angular.injector(["ng"]).get("$http");
    
30.03.2017 / 20:37
-1

There is a hack for this:

angular.module('your-module').run(['$http',function($http){
    window.$http = $http;
}];

So you can use $ http anywhere by calling window. $ http since the call is made after the angular start.

    
05.10.2016 / 13:40