0

In the following plunkr, can anyone explain why I have to inject 'ui.router' into the first listed submodule, but not any of the subsequent submodules?

http://plnkr.co/edit/nCBeMAKrxvL9CHvLzRdL?p=preview

I thought the ui.router dependency would be available to every submodule after doing this: angular.module('myapp', ["myapp.route1", "myapp.route2", "ui.router"])

Instead, i have to include ui.router in the first listed dependency (in this case myapp.route): angular.module('myapp.route1', ["ui.router"])

but not in myapp.route2: angular.module('myapp.route2', [])

What am I missing?

1 Answer 1

1

The loading order depends on the order of declared dependencies, so

angular.module('myapp', ["myapp.route1", "myapp.route2", "ui.router"])

means that "myapp.route1" loads first (in config phase - this doesn't apply to run-phase), and it also loads "ui.router". And, so, when "myapp.route2" loads, "ui.router" service are already loaded.

Changing the order of declared dependencies breaks this.

So, clearly, you should not rely on this. Each module should declare its own dependencies.

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

4 Comments

how would this work if you were writing a ui framework, such as ui.bootstrap? when using it, you only have to declare ui.bootstrap as a dependency due to the fact that all other dependencies (ui.bootstrap.accordion, ui.bootstrap.alert, etc.) are available via ui.bootstrap. At least that's what i can gather from the source code.
I'm not sure I understand the question. If you depend on module "Foo" you need to declare it. Module "Foo", in turn, can have any number of dependencies which you shouldn't care about - Angular would resolve them accordingly. So, in your question, "myapp.route2" depends on "ui.router" (since it uses $stateProvider), so you should declare it and not rely on "myapp.route1" to include it.
so if i add a module as a dependency, that module and its dependencies are accessible, correct? in this case, ui.router and route.1 are both dependencies of the same module and should therefore be injected...?
@CoryDorning, I'm not sure what you're asking... "...therefore be injected" into what? Why complicate? Each module just needs to reason about its own immediate dependencies. route1 uses ui.router, route2 uses ui.router, myapp uses route1, route2, and ui.router - so, each module declares what it uses. Don't make assumptions about what other modules depend on and whether they were loaded

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.