【问题标题】:Angular JS TypeError: $http is not a functionAngular JS TypeError:$http 不是函数
【发布时间】:2016-07-11 08:01:45
【问题描述】:

我已经阅读了人们遇到此问题的所有帖子,其中 $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) {

        });
    };
}
])

任何建议将不胜感激。

【问题讨论】:

  • 试试这个 $scope.makeCall= function () { ...

标签: javascript angularjs promise angularjs-controller angularjs-http


【解决方案1】:

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) {

      }
   );
};

【讨论】:

    猜你喜欢
    • 2016-10-02
    • 2016-06-19
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多