Angular JS TypeError:$http不是函数

Angular JS TypeError: $http is not a function

本文关键字:函数 http JS TypeError Angular      更新时间:2023-09-26

我已经通读了所有的帖子,在这些帖子中,人们遇到了这个问题,其中$http不是一个函数,而且在大多数情况下,这似乎是由于注入顺序错误。

我的模块定义如下:

angular.module("app", []).controller("appCtrl", ['$scope','$http',
    function ($scope, $http) {
...
    $scope.makeCall= function ($http) {
         console.log("HERE");
         $http({ method: 'GET', url: <url }).
            then(function (response) {
                console.log(response.data);
                return response.data;
            }, function (response) {
        });
    };
}
])

如有任何建议,我们将不胜感激。

makeCall函数中删除$http参数,这将扼杀通过控制器注入的$http依赖关系的存在。基本上,当你在函数上添加它时,它被设置为undefined

$scope.makeCall= function () { //<-- removed $http dependency from here
   console.log("HERE");
   $http({ method: 'GET', url: 'url' })
      .then(function (response) {
            console.log(response.data);
            return response.data;
      }, function (response) {
      }
   );
};