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";
    }
  );

}]);

ここで何が間違っていますか?どうすれば修正できますか?

4

1 に答える 1

20

ブラケット表記を使用する場合、関数の前の依存関係リストは、関数に注入されるサービスと一致する必要があります。

関数に追加の$locationサービスがあるEventsListControllerため、これを変更します。

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

于 2013-07-07T21:11:59.080 に答える