11

I get TypeError: Cannot call method 'get' of undefined when running this module:

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

}]);

What am I doing wrong here and how can I fix it?

1 Answer 1

20

When using the bracket notation the dependency list before the function needs to match the services being injected into the function.

You have an extra $location service in your EventsListController function so change this:

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

to this:

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

The key change being: function EventListController($scope, $http) instead of function EventListController($scope, $location, $http)

Sign up to request clarification or add additional context in comments.

1 Comment

I wish I could upvote this answer twice! This wasn't my exact cause, but helped. My editor failed to update my .min file, which is only used in production, so I spent quite a while trying to figure out why my deployment broke production and no other environment. Thanks for saving my weekend!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.