4

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?

Andre Soares
  • 1,518
  • 2
  • 18
  • 24

1 Answers1

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

Community
  • 1
  • 1
Poul Kruijt
  • 58,329
  • 11
  • 115
  • 120