1

I have a module 'moduleA' that has 2 services, and one(serviceB) is depending on the other one(serviceA). I am trying to inject serviceB into a controller which belongs to a different module 'foo', however I got unknown provider error. Below is my code:

module.js

angular.module('moduleA', []);

factory-a.js

angular
.module('moduleA')
.factory('factoryA', factoryA);

factoryA.$inject = ['$q', '$log', '$timeout'];

function factoryA($q, $log, $timeout) {
//Do Stuff
}

factory-b.js

angular
.module('moduleA')
.factory('factoryB', factoryB);

factoryB.$inject = ['factoryA'];

function factoryB(factoryA) {
//Do Stuff
}

foo-controller.js

angular.module('foo', ['moduleA'])
       .controller('fooController', ['factoryB', function(factoryB){
//Do Stuff
})

Error in Console:

generic-console-medium.js:23 2016-11-27 18:49:42.395 - [$injector:unpr] Unknown provider: factoryBProvider <- factoryB <- fooController
http://errors.angularjs.org/1.5.5/$injector/unpr?p0=factoryBProvider%20%3C-%20factoryB%20%3C-%20fooController  Error: [$injector:unpr] Unknown provider: factoryBProvider <- factoryB <- fooController
http://errors.angularjs.org/1.5.5/$injector/unpr?p0=factoryBProvider%20%3C-%20factoryB%20%3C-%20fooController

at http://localhost:9001/components/angular/angular.js:68:12

at http://localhost:9001/components/angular/angular.js:4458:19

at Object.getService [as get] (http://localhost:9001/components/angular/angular.js:4611:39)

at http://localhost:9001/components/angular/angular.js:4463:45

at getService (http://localhost:9001/components/angular/angular.js:4611:39)

at injectionArgs (http://localhost:9001/components/angular/angular.js:4635:58)

at Object.invoke (http://localhost:9001/components/angular/angular.js:4657:18)

at $controllerInit (http://localhost:9001/components/angular/angular.js:10115:34)

at nodeLinkFn (http://localhost:9001/components/angular/angular.js:9033:34)
    at http://localhost:9001/components/angular/angular.js:9433:13
2
  • Could, you please post the error (textual from console) you are getting? Commented Nov 27, 2016 at 23:32
  • @AsielLealCeldeiro updated with console error, thanks! Commented Nov 27, 2016 at 23:58

1 Answer 1

2

This might sound trivial but Did you include your factory-b.js file in your project?

I think you should fix the code shown below. Everything looks fine except for this.

function factoryA($q, $log, $timeout) { //remove the ''
//Do Stuff
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing out, that was a typo, I have removed quotes in the code above. I am using requireJS in the project, I only included the module.js in my controller, it seems I need to include both module.js and factory-b.js to make it work.

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.