6

I have say two modules:

  1. foo.a
  2. foo.b

and an application module:

  angular.module("foo", ["foo.a","foo.b"])

I have a service in module foo.b say:

  angular.module("foo.b", [])

  angular.module("foo.b").factory("helper",helperFn);

which I want to use in one of my controllers in foo.a.

What I have done is simple dependency injection:

 angular.module("foo.a", []);

 angular.module("foo.a")
        .controller("MyController",["helper",MyControllerFn]);

which is working.

My questions are

  1. How am I getting the "helper" service from module foo.b even though it is not declared as a dependency for module a?
  2. Will it break at a later stage?
  3. If it is correct, is this a good practice?
2
  • I am afraid you will get an error Error: [$injector:unpr] Unknown provider: helperProvider <- helper. Or put some JSFiddle to illustrate your issue. Commented Aug 25, 2015 at 4:48
  • the issue is that i am able to use a factory from other module without adding that module as a dependency plunker Commented Aug 25, 2015 at 6:42

1 Answer 1

7

Put the factory that you need access to in both modules in a third module. Have your two original modules inject a dependency to the third module.

angular.module("foo", ["foo.a", "foo.b"]);

angular.module("foo.a", ["foo.c"])
  .controller("MyController", ["helper", MyControllerFn]);

angular.module("foo.b", ["foo.c"]);

angular.module("foo.c")
  .factory("helper", helperFn);
Sign up to request clarification or add additional context in comments.

Comments

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.