6

I want to forget "jQuery" because i like "AngularJS". However, i need to know how to use independent tasks that incorporate AngularJS elsewhere in my application. On this occasion I want to use "$https AngularJS" function to import a JavaScript file for example.

Example (it used to do in jQuery):

$.get("url.js", function(data){ eval(data) }); //ok
console.info($.get); //code code code... ok

Example (as documented in AngularJS)

//In a controller
App.controller('Ctrllr', ['$http', function ($http) {
    $http.get("url.js").success(function(data){
        eval(data); //ok
    });
    console.info($http); //code code code.... ok
})

//outside
$http.get("url.js"); //$http is undefined
//How to use $http here?

As you see in the last call, $http is outside of a process. Now, would like to know, how to use the class $http or another Angular utils outside of a controller/application?

3
  • what you mean by outside? before a controller is loaded? on which event related to angular context? Commented Feb 7, 2015 at 13:35
  • before a controller is loaded Commented Feb 7, 2015 at 15:00
  • Do the answers provided not satisfy your request? Commented Feb 7, 2015 at 17:28

4 Answers 4

5

Use this:

$http = angular.injector(["ng"]).get("$http");
Sign up to request clarification or add additional context in comments.

Comments

4

Surely you can use $http within a run block

angular.module('myModule', [])
.run(function($http) {$http.get('/url').success(mySuccessCallback)}

But you still have to use $http in angular context (application) because dependency injection is needed to inject $http service

Comments

1

Thank you all, I think the only solution would be to declare a global variable and then assign a controller $https for this variable

var myAjax;
App.controller('Ctrllr', ['$http', function ($http) {
  myAjax = $http;
});

myAjax("url.js").success(function(code){eval(code)}

If anyone has a better solution is welcome. A greeting :)

Comments

0

You can't do that. But that's fine, you shouldn't.

What you should do is setup a service using a factory which can then be accessed by your controllers or whatever you want. A factory looks like this...

app.factory('service', ['$http', function($http) {
   var service = {}

   service.get = function() {
     return $http.get('/url');
   }
   return service.
 }]);

Then you can inject this service into anything you want and can call the get function from there which will return the promise for you.

Edit: to clarify, you don't have to return the promise you can use your imagination.

3 Comments

Because you CAN do that, while yes, you shouldn't. Also making a factory to access a service just ridiculous. Read his question next time.
Good advice, you should read it too! In the way he wrote it, it is not possible. You could use some kind of http service outside of an angular controller, but not like that. I don't think ridiculous is the word you are looking for. This is the standard way to do this, it makes it modular and clean/organized.
He wants to use service outside all the providers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.