5

I want to get the metadata for the current NgModule in order to get the list of declarations and providers in order to fill in a dynamic module I create to display components in a modal.

How can that be done?

1 Answer 1

1

You can access the declarations using the reflect-metadata package. You do however need to install this package and include it in your project. After that you can get the annotations like this:

let annotations: DecoratorFactory[] = Reflect.getMetadata('annotations', ModuleClass);

If the @NgModule is the only annotation on there, which I would guess it is, you can access the declarations like this, otherwise you have to guess the right index:

let declarations: any[] = annotations[0].declarations;

See this answer for further reference

3
  • And what if I want to get the current module without knowing which one is it? Jan 27, 2017 at 13:39
  • Unfortunately, there is no such thing as a current module. Because your application can consist of multiple NgModules at once. What are you trying to do exactly? Jan 27, 2017 at 13:55
  • If you can give plnkr.co implementation that will be great
    – Ali Abbas
    May 25, 2017 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.