3

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

1 Answer 1

4

You forgot to inject your service. Try this:

myApp.controller('homeController', ['$scope', 'myService', function($scope, myService) {
  $scope.projects = myService.getProjects();
  $scope.message = "homeController";
}]);
Sign up to request clarification or add additional context in comments.

1 Comment

Nice answer @Florian, This was getting the better of me. I keep forgetting to inject the service now that I am using the brackets to preserve code for minification.

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.