AngularJS $ http未定义

Hig*_*ife 10 module http angularjs

TypeError: Cannot call method 'get' of undefined在运行这个模块时得到:

angular.module('EventList', [])

.config([ '$routeProvider', function config($routeProvider){
    $routeProvider.when(Urls.EVENT_LIST_PAGE, {
        templateUrl: 'app/EventList/event-list.html',
        controller: 'EventListCtrl'
      });
 }])


.controller('EventListCtrl', ['$scope', '$http', function EventListController($scope, $location, $http) {
  $scope.events = [];
  $http.get('http://localhost:8000/event').
    success(function (data, status) {
      $scope.events = data;
      for (var i = 0; i < $scope.events.length; i++) {
        $scope.events[i].event_url = ('#' + Urls.EVENT_PAGE + '/' + $scope.events[i]._id);
      }
    }).
    error(function (data, status) {
      $scope.data = data || "Request failed";
    }
  );

}]);
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么,我该如何解决?

Glo*_*opy 19

使用括号表示法时,函数之前的依赖关系列表需要匹配注入函数的服务.

你的功能有额外的$location服务,EventsListController所以改变这个:

.controller('EventListCtrl', ['$scope', '$http', function EventListController($scope, $location, $http) {
// controller code goes here
}]);
Run Code Online (Sandbox Code Playgroud)

对此:

.controller('EventListCtrl', ['$scope', '$http', function EventListController($scope, $http) {
// controller code goes here
}]);
Run Code Online (Sandbox Code Playgroud)

关键的变化是:function EventListController($scope, $http)而不是function EventListController($scope, $location, $http)