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) {
StarTrek18
  • 323
  • 4
  • 15

1 Answers1

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.

Brocco
  • 62,737
  • 12
  • 70
  • 76
  • Wow, so this is interesting. What if you are loading external modules and you don't have control on naming?? – StarTrek18 Apr 07 '14 at 19:34
  • 2
    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. – Brocco Apr 07 '14 at 19:36