user3334619
user3334619

Reputation: 33

Angular JS Factory -- undefined is not an object issue

I am new to Angular JS. Here is my issue. I am trying to create a factory . But when I am calling factory it gives me an error -Error undefined is not an object (evaluating: myService.getProjects)

Code:

myApp.factory('myService', function() { 
  return {
    getProjects: function() {
        return "Test";
    }
  };
});

myApp.controller('homeController',['$scope',function($scope,myService)
{
 $scope.projects=myService.getProjects();
 $scope.message="homeController";
}]);

Upvotes: 3

Views: 3287

Answers (1)

Florian
Florian

Reputation: 3366

You forgot to inject your service. Try this:

myApp.controller('homeController', ['$scope', 'myService', function($scope, myService) {
  $scope.projects = myService.getProjects();
  $scope.message = "homeController";
}]);

Upvotes: 4

Related Questions