0

I created a simple service and a simple directive that use that service. Here is how it looks like:

angular.module("app",[]);
angular.module("app").factory('dummyService',dummyService);
angular.module("app").directive('dummyDirective', ['dummyService',dummyDirective]);

function dummyService(){
    this.name = "hello";
}

function dummyDirective(dummyService) {
    return {
        link:function(scope){
            console.log(dummyService.name);
        }
    }
}

However, when I run the code I get:

[$injector:undef] http://errors.angularjs.org/1.4.7/$injector/undef?p0=dummyService

Any suggestions?

RiskX
  • 407
  • 4
  • 17

2 Answers2

4

You should use service instead of factory. In your code change

angular.module("app").factory('dummyService',dummyService);

to

angular.module("app").service('dummyService',dummyService);

if you want to use factory, you need to return object in the factory function:

function dummyService(){
   return {
     name : "hello";
   }  
}

Demo on Jsfiddle

See also: confused about service vs factory

Community
  • 1
  • 1
giladk
  • 176
  • 6
-1

You don't pass the dependency for the directive:

angular.module("app").directive('dummyDirective', ['dummyService', dummyDirective(dummyService)]);
Marios Fakiolas
  • 1,505
  • 1
  • 12
  • 19
  • You don't pass arguments to a function on that line because we don't execute the function there only passing it as a parameter(Angular suppose to pass the argument when it execute the function). Any way I tried your way just to be safe but it didn't help. Any more ideas? Thanks! – RiskX Nov 10 '15 at 16:58