Reputation: 8599
when calling dialog.open from Injectable Service it fails with Error
core.js:7187 ERROR Error: Uncaught (in promise): Error: No component factory found for LessonEndedDialogComponent. Did you add it to @NgModule.entryComponents?
Error: No component factory found for LessonEndedDialogComponent. Did you add it to @NgModule.entryComponents?
@Component()
export class LessonEndedDialogComponent {}
@NgModule({
declarations: [LessonEndedDialogComponent],
entryComponents: [LessonEndedDialogComponent],
})
export class CoursesModule { }
@NgModule({
declarations: [AppComponent,],
bootstrap: [AppComponent]
})
export class AppModule { }
const routes: Routes = [
{
path: 'courses',
loadChildren: () => import('./courses/courses.module').then(mod => mod.CoursesModule)
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
@Injectable({
providedIn: 'root'
})
export class NavigationService {
constructor(public dialog: MatDialog) { }
displayDialog() {
this.dialog.open(LessonEndedDialogComponent);
}
}
change
providedIn: 'root'
to
providedIn: CoursesModule
Circular dependency detected:
Cannot access 'CoursesModule' before initialization
becase
providedIn: 'root'
move
entryComponents: [
LessonEndedDialogComponent
],
to Root
AppModule
but LessonEndedDialogComponent is form sub Module
this will be make the root Module dependen on the sub Module
witch is bad for separation of Modules
Upvotes: 1
Views: 173
Reputation: 8599
change
@Injectable({
providedIn: 'root'
})
to
@Injectable()
use providers array on the Sub Module
@NgModule({
declarations: [LessonEndedDialogComponent],
entryComponents: [LessonEndedDialogComponent],
providers: [NavigationService],
})
export class CoursesModule { }
Upvotes: 1