Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 the ReferenceError: $http is not defined in my console. Can anyone help me understand what i am doing wrong here.

share|improve this question

2 Answers 2

up vote 222 down vote accepted

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
}
share|improve this answer
12  
Thanks! I wonder why Angular's own documentation (docs.angularjs.org/tutorial/step_05) has this error. –  Anurag Oct 9 '13 at 11:49

I have gone through the same problem when I was using

    myApp.controller('mainController', ['$scope', function($scope,) {
        //$http was not working in this
    }]);

I have changed the above code to given below. Remember to include $http(2 times) as given below.

 myApp.controller('mainController', ['$scope','$http', function($scope,$http) {
      //$http is working in this
 }]);

and It has worked well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.