TypeError: Cannot read property 'get' of undefined

TypeError: Cannot read property 'get' of undefined

$http.get('/contactList').
success(function(data){
    console.log('got http get');
}).
error(function(data) {
    $scope.error = true;
    $scope.data = data;
    return 'error name';
});

我只有这一段代码有错误。我正在尝试将 $http.get 函数与 Angular JS 一起使用。我做错了什么吗?

我一直收到错误消息。

正如其他评论者所说,您应该查看您的控制器声明,您需要 'inject' $http 服务:

angular.module('myApp')
.controller('messageController', ['$scope', '$http', function($scope, $http){
  $http.get()
  .success()
  .error();
}]);

由于我没有看到任何控制器代码,因此我不得不假设以下原因之一可能导致此问题,

  1. 您没有像评论员和@Plato 指出的那样在控制器中注入所有依赖项。检查这个 -> TypeError: Cannot call method 'get' of undefined

  2. 你注入了所有的依赖,但是顺序不对,比如应该是['$scope', '$http', function($scope, $http),你会这样说['$scope', '$http', function($http, $scope)。当使用数组符号来注入依赖时,参数的顺序很重要。检查这个 ->