1

Say I have two modules (finance2, finance3), and each of them defines a service with the same name (currencyConverter).

If I tell my main module that it depends on just finance2, I can inject the service like this:

angular.module('invoice2', ['finance2'])
  .controller('InvoiceController', ['currencyConverter', function(currencyConverter) {

However, if I want my invoice2 to depend on both modules, which currencyConverter would be injected? The one from finance2 or the one from finance3? I can control my own modules, but my concern is if you rely on other people modules that define factories with the same name. How does angular deal with that?

angular.module('invoice2', ['finance2','finance3'])
  .controller('InvoiceController', ['currencyConverter', function(currencyConverter) {

1 Answer 1

3

The last one loaded/processed will win.

If your scripts are:

  • finance2 (with currencyConverter)
  • finance3 (with currencyConverter)

Then you will get finance3's currencyConverter when the dependency for currencyConverter is resolved.

If your scripts are:

  • finance3 (with currencyConverter)
  • finance2 (with currencyConverter)

Then you will get finance2's currencyConverter when the dependency for currencyConverter is resolved.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, so this is interesting. What if you are loading external modules and you don't have control on naming??
I believe this is a known short-coming of angular's current DI framework and they are aware of it, which is part of the reason they are implementing DI differently in version 2.

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.