Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get bootstraped component

I have a simple @NgModule the boostrap 2 components :

// Global module for all Angular Component
@NgModule({
    declarations: Components, // directives, components, and pipes owned by this NgModule
    imports: [BrowserModule, HttpModule, FormsModule],
    providers: Providers,
    bootstrap: [Menu, Main]
})
export class MenuModule { }

I want to store a reference of my Main component bootstraped by my MenuModule. I tried to implement the .then method of bootstrapModuleDynamic(), but I only get the factory of the component.

// Bootstrap of module
platformBrowserDynamic()
    .bootstrapModule(MenuModule)
    .then((menuModule: NgModuleRef<MenuModule>) => {
        var test: ComponentFactory<Main> = (<any>menuModule).bootstrapFactories[0];
    });

Any ideas ?

like image 474
Christophe Gigax Avatar asked Jul 14 '26 11:07

Christophe Gigax


1 Answers

I see two ways to get list of bootstraped components:

1) Using ApplicationRef

platformBrowserDynamic()
    .bootstrapModule(MenuModule)
    .then((menuModule: NgModuleRef<MenuModule>) => {
        const appRef = menuModule.injector.get(ApplicationRef);
        const components = appRef.components;      
    });

2) Using ngDoBootstrap method on your module:

@NgModule({
    declarations: [Components, // directives, components, and pipes owned by this NgModule
    imports: [BrowserModule, HttpModule, FormsModule],
    providers: Providers,
    entryComponents: [Menu, Main]
    // bootstrap: [Menu, Main]
})
export class MenuModule {
   ngDoBootstrap(appRef: ApplicationRef) {
     const compRefMenu = appRef.bootstrap(Menu);
     const compRefMain = appRef.bootstrap(Main);
   }
}
like image 169
yurzui Avatar answered Jul 17 '26 22:07

yurzui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!