user2727195
user2727195

Reputation: 7330

get $http service and call

How to get the $http service in angular js and then call get function on it inside my custom asyncAction function call. There's no html involved (bootstrap???)

Edit: Can put some dummy html to make it work to trigger the controller something

I've a jsfiddle at http://jsfiddle.net/smartdev101/bdmkvr6g/

    asyncAction: function(resultFunction, faultFunction) {

    $http.get("https://api.github.com/users/angular")
    .success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
        console.log(data);
    })
    .error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    });
},

Some progress - updated fiddle, added, some html tag to make it work, but

can someone find me a way without having to insert markup in my code and even triggering AttendeeProxyController myself without this ng-controller tag ???? I'd prefer full manual boot and controller trigger process.

OR lateral thinking... I don't event need this controller, it's there so that I can get access of $http purely just in javascript without html?

<span id="attendeeProxyController" ng-controller="AttndeeProxyController"></span>

Upvotes: 1

Views: 156

Answers (2)

Tyler.z.yang
Tyler.z.yang

Reputation: 2450

Like @Benjamin said, you can use the invoke to auto inject dependency to you function.

var $injector = angular.injector(["ng"]);
$injector.invoke(function($http){
    $http.get("https://api.github.com/users/angular").success(function(data){
        console.log(data);
    }).error(function(data){
        console.log(data);
    });
});

Here is $injector document.

Upvotes: 0

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276306

OR lateral thinking... I don't event need this controller, it's there so that I can get access of $http purely just in javascript without html?

Yes, while I recommend against it it's possible to do

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

This will get you a direc reference to $http which you can use. However it's against the Angular way of dependency injection, it'll generally produce harder to test and harder to reason about code if you're not careful.

Upvotes: 2

Related Questions