How do I introspectively discover defined directives (or controllers, etc) in an included Angular module?

377 views Asked by At

I'm architecting a project where we have one module acting as our shell, handling global styles, navigation, etc, with an ng:view getting filled by directives provided by our second module with all the pages.

I would really like to find a way for the shell to inspect the pages module, discovering the nav structure dynamically, rather than defining it by hand or having every page inject itself both in the pages module AND to the shell module. On its own, the injector doesn't seem to like doing that.

EDIT

As clarification: while I have a fabulous workaround with a pagesProvider service, I am still looking for an answer as to whether Angular 1.2.x has a mechanism to introspect modules.

1

There are 1 answers

5
artur grzesiak On

Firstly: at the end all injected objects (like services, filters, controllers) will end up in one big bag (something like global namespace). So it is enough to inject each page into pages and pages into shell. (mind the possibility of name-conflicts.)

Secondly: at this time, there is no support for lazy loading of modules. So you should know all your modules beforehand, so there is actually no need to check which objects are present and which not, as either there are all of them or the application has not started at all.

I do not know what you actually mean by discovering nav structure and how it will be defined. But, what you can do is:

  1. in each page-module define a constant in which the nav structure of this page will be stored.
  2. access this constant from shell when needed. If you need to know dynamically the idof active page, just set this information somewhere -- probably as value -- when the pages gets activated.

Some alternatives:

  1. define one global hash-map from page-ids to page-nav-structures.
  2. use a common service that will be injected to each page and will be used to register pages and all related information about it.