AngularJs ReferenceError: $http is not defined

Chubby Boy picture Chubby Boy · Dec 7, 2012 · Viewed 157.6k times · Source

I have the following Angular function:

$scope.updateStatus = function(user) {    
    $http({
        url: user.update_path, 
        method: "POST",
        data: {user_id: user.id, draft: true}
    });
};

But whenever this function is called, I am getting ReferenceError: $http is not defined in my console. Can someone help me understanding what I am doing wrong here?

Answer

ŁukaszBachman picture ŁukaszBachman · Dec 7, 2012

Probably you haven't injected $http service to your controller. There are several ways of doing that.

Please read this reference about DI. Then it gets very simple:

function MyController($scope, $http) {
   // ... your code
}