@angular/core#createNgModuleRef TypeScript Examples

The following examples show how to use @angular/core#createNgModuleRef. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: route.component.ts    From router with MIT License 6 votes vote down vote up
private loadAndRender(route: Route) {
    if (route.load) {
      return from(
        route
          .load()
          .then(
            (componentOrModule: NgModuleRef<ModuleWithRoute> | Type<any>) => {
              let component: Type<any>;

              if ((componentOrModule as any).ɵmod) {
                const moduleRef: NgModuleRef<ModuleWithRoute> =
                  createNgModuleRef(
                    componentOrModule as Type<any>,
                    this.viewContainerRef.injector
                  );
                component = moduleRef.instance.routeComponent;
              } else {
                component = componentOrModule as Type<any>;
              }

              this.renderComponent(component);
            }
          )
      );
    } else {
      this.showTemplate();
      return of(true);
    }
  }
Example #2
Source File: page.service.ts    From angular-miniprogram with MIT License 6 votes vote down vote up
register() {
    this.app.__ngStartPage = <M, C>(
      module: Type<M>,
      component: Type<C>,
      miniProgramComponentInstance: MiniProgramComponentInstance
    ) => {
      return this.ngZone.run(() => {
        const injector = Injector.create({
          providers: [
            { provide: PAGE_TOKEN, useValue: miniProgramComponentInstance },
          ],
          parent: this.injector,
        });
        const ngModuleRef = createNgModuleRef(module, injector);
        const componentFactory =
          ngModuleRef.componentFactoryResolver.resolveComponentFactory(
            component
          );
        const componentRef = componentFactory.create(injector);
        this.applicationRef.attachView(componentRef.hostView);
        return { componentRef, ngModuleRef };
      });
    };
  }