AngularJs ReferenceError: $http is not defined

前端 未结 3 1145
不思量自难忘°
不思量自难忘° 2020-11-29 21:22

I have the following Angular function:

$scope.updateStatus = function(user) {    
    $http({
        url: user.update_path, 
        method: \"POST\",
              


        
相关标签:
3条回答
  • 2020-11-29 21:49

    Just to complete Amit Garg answer, there are several ways to inject dependencies in AngularJS.


    You can also use $inject to add a dependency:

    var MyController = function($scope, $http) {
      // ...
    }
    MyController.$inject = ['$scope', '$http'];
    
    0 讨论(0)
  • 2020-11-29 21:57

    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
    }
    
    0 讨论(0)
  • 2020-11-29 22:00

    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.

    0 讨论(0)
提交回复
热议问题