Angular Dialog: No component factory found for DialogModule.

123 picture 123 · Jun 12, 2018 · Viewed 13k times · Source

Trying to make a dialog using feature model in angular 6. But I'm getting this error:

No component factory found for DialogModule. Did you add it to @NgModule.entryComponents?

Everyone keeps saying to use

entryComponents: [DialogComponent]

which I am already doing. Also tried to use that in the feature module without success. Here are I think the necessary and simplified files:

app.module.ts

import { DialogModule } from './components/dialog/dialog.module';
import { DialogComponent } from './components/dialog/dialog.component';
...

// AoT requires an exported function for factories
export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http);
}

@NgModule({
  declarations: [..., AppComponent],
  imports: [DialogModule],
  entryComponents: [DialogComponent],

  providers: [..., MatDialogModule],
  bootstrap: [AppComponent]
})
export class AppModule {}

dialog.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { DialogComponent } from './dialog.component';
...

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [..., DialogComponent],
  exports: [DialogComponent]
})
export class DialogModule {
  ...
}

some-other.component.ts

import { DialogModule } from '../../components/dialog/dialog.module';
...

@Component({
    ...
})

export class LanguageButtonComponent implements OnInit {

  constructor(private languageService : LanguageService,
    private dialog: MatDialog,) {
  }

  // activated from button
  openDialog() {
    this.dialog.open(DialogModule);
  }
}

How to get rid of the error?

Answer

Bruno João picture Bruno João · Jun 12, 2018

You have to put your DialogComponent in entryComponents of DialogModule and not in AppModule:

  1. Place the entryComponents in the correct module

    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';

    import { DialogComponent } from './dialog.component';
    ...

    @NgModule({
      imports: [
        CommonModule
      ],
      declarations: [DialogComponent],
      exports: [DialogComponent],
      entryComponents: [DialogComponent],
    })
    export class DialogModule {
        ...
    }

  1. Remove entryComponents from AppModule