How to use an angular service outside the angular structure?

Asked

Viewed 73 times

1

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

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

And if I want to use the $http outside the controller or config? Has as?

There is 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 () {

})
  • Where exactly do you want to use this? You can cite a practical example?

  • When I went to make a plugin for Chrome, I had to do this. Use HTTP "out of Angular". I don’t want to depend on $.ajax jQuery, but I really needed to use "globally" the $http. It may be that I got it wrong in my structure, but in Chrome’s "background.js" there was no reason to have one app.module. I really just needed the $http (not to use jQuery).

  • I particularly do not know a method of using not only $http, but any other Angular service outside its scope, due to the structural and event "dependencies" it has.

  • I actually managed to do this using the angular feature itself. I just wanted to know if there was a better way. I got it like this var $http = angular.injector(["ng"]).get("$http");

  • @In this specific case, see: http://pastebin.com/wGCAucTs

  • 1

    Humm.. Interesting!!! I use this feature nowadays, but in reverse. I make lazyload of some module that is only necessary in a certain view and then make the injection of it INSIDE the angular. But good to know you can make out of it too!

  • That’s why I denied the answer below. I found very strange the way it was made.

Show 3 more comments

2 answers

0

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

var $http = angular.injector(["ng"]).get("$http");

-1

There’s a hack for that:

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

So Voce can use $http anywhere by calling window. $http as long as the call is made after angular startup.

  • Bad idea. If I needed this in a context other than global, how would it be done? In addition to what Angular seems to offer a more elegant solution to this.

  • Actually Guilherme, the $http service can be used in any area of Angularjs, it’s not like $Scope that has limitations. Ex.: factory('minhaFactory', function($scope) {}); This is not possible. So your solution is a bit 'redundant' since it remains within the scope of Angularjs.

  • @Wallacemaxters The idea is to be able to use outside the angular context in some kind of migration to the angular where some area that has not yet been migrated needs to access the services done at the angular for some reason.

  • @Celsomtrindade The idea is not to abandon the angular but to provide a service that is only available through injection of dependency within the angular to "legacy" areas of an application that are outside the angular.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.