Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use $http outside of a controller? in AngularJS

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?

like image 410
Walter Chapilliquen - wZVanG Avatar asked Feb 07 '15 03:02

Walter Chapilliquen - wZVanG


People also ask

How do you call a scope function outside the controller?

The function would be used as so: var $scope = getScope('ctrl'); Now, the above $scope variable is the exact same one that is inside of the "ctrl" controller.

How do you modify the $HTTP request default Behaviour?

To add or overwrite these defaults, simply add or remove a property from these configuration objects. To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g. $httpProvider. defaults.

What is $HTTP in AngularJS?

$http is an AngularJS service for reading data from remote servers.

How do we pass data and get data using http in angular?

Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.


1 Answers

Use this:

$http = angular.injector(["ng"]).get("$http");
like image 106
Tatiana Perere Avatar answered Oct 10 '22 06:10

Tatiana Perere