AngularJS $http未定义。

11

运行这个模块时,我得到了 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";
    }
  );

}]);

我在这里做错了什么,我该怎么修复它?

1个回答

20

在使用方括号表示法时,函数之前的依赖列表需要与注入到函数中的服务相匹配。

您的EventsListController 函数中有一个多余的$location 服务,请将其更改为:

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

变成这样:

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

重要更改在于: function EventListController($scope, $http),而不是function EventListController($scope, $location, $http)


1
我希望我能给这个答案点赞两次!虽然这不是我的确切原因,但它确实有所帮助。我的编辑器未能更新我的.min文件,该文件仅在生产中使用,因此我花了很长时间来弄清楚为什么我的部署破坏了生产环境而没有影响其他环境。感谢您挽救了我的周末! - Johnie Karr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接