0

I want to dynamically process the module

It is not only drawing the components in the demoule on the demo, but also drawing the components in the imports

I want to do it in ViewChild except for the impont module. I want to open the moduleType in the ngModuleFactory. I have an error.

this.module

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'
import { FlightBookingDetailComponent } from './flight-booking-detail.component';
import { TabModule } from 'app/shared/custom-directives/tab/tab.module';

@NgModule({
  declarations: [FlightBookingDetailComponent],
  imports: [
    CommonModule,
    TabModule
  ]
})
export class FlightBookingDetailModule { }

ModalComponent.ts

import {Compiler, Component, Type, Input, Injector, NgModule, NgModuleRef, ComponentFactoryResolver, ViewChild, ViewContainerRef, NgModuleFactory} from "@angular/core";
import { ModalDirective } from './modal.directive';

@Component({
    selector: 'agent-modal',
    template: '<div #vc></div>'
})
export class ModalComponent {
  @Input() component: Type<any>;
  @Input() module: Type<any>;

  @ViewChild('vc', {read: ViewContainerRef}) container: ViewContainerRef;

  constructor(private compiler: Compiler,
              private componentFactoryResolver: ComponentFactoryResolver,
              private injector: Injector,
              private ngModuleRef: NgModuleRef<any>) {
  }

  ngAfterViewInit() {
    const template = '<span>I am {{name}}</span>';

    console.log('aaa', this.module)
    this.compiler.compileModuleAndAllComponentsAsync(this.module)
    .then((factories) => {
      console.log('asfadf', factories)
      const moduleType = factories.ngModuleFactory.moduleType() << error



      for (let componentFactories of factories.componentFactories) {
        const cmpRef = componentFactories.create(this.injector, [], null, this.ngModuleRef);

        console.log('cmpRef', cmpRef)

        this.container.insert(cmpRef.hostView);
      }
    })
  }
}

error :' Value of type 'Type' is not callable. Did you mean to include 'new'?ts(2348) '

김도균
  • 29
  • 3
  • 1
    To display the image in your post, edit the first two lines to read `![enter image description here][1] I want to dynamically process the module` – Philipp Gfeller Feb 25 '19 at 07:31
  • 1
    *I have an error* - what error? [edit] your question and include full error message – barbsan Feb 25 '19 at 07:45

0 Answers0