Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get metadata for the current module in Angular 2?

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?

like image 917
Andre Soares Avatar asked Nov 08 '22 02:11

Andre Soares


1 Answers

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

like image 52
Poul Kruijt Avatar answered Nov 15 '22 07:11

Poul Kruijt