I am trying to follow the docs on https://material.angular.io/components/component/dialog but I cannot understand why it has the below issue?
I added the below on my
I had the same issues and i had dialogComponent in EntryComponents and it still did not work. this is how I was able to solve the problem. the link is here to a previously answered post:
https://stackoverflow.com/a/64224799/14385758
If you're trying to use MatDialog
inside a service - let's call it 'PopupService'
and that service is defined in a module with:
@Injectable({ providedIn: 'root' })
then it may not work. I am using lazy loading, but not sure if that's relevant or not.
You have to:
PopupService
directly to the component that opens your dialog - using [ provide: PopupService ]
. This allows it to use (with DI) the MatDialog
instance in the component. I think the component calling open
needs to be in the same module as the dialog component in this instance.matDialog
when you call your service.Excuse my jumbled answer, the point being it's the providedIn: 'root'
that is breaking things because MatDialog needs to piggy-back off a component.
If you are like me, and are staring at this thread thinking "But I'm not trying to add a component, I am trying to add a guard/service/pipe, etc." then the issue is likely that you have added the wrong type to a routing path. That is what I did. I accidentally added a guard to the component: section of a path instead of the canActivate: section. I love IDE autocomplete but you have to slow down a bit and pay attention. If you absolutely can't find it, do a global search for the name it is complaining about and look at every usage to make sure you didn't slip-up with a name.
In case of lazy loading, you just need to import MatDialogModule in lazy loaded module. Then this module will be able to render entry component with its own imported MatDialogModule:
@NgModule({
imports:[
MatDialogModule
],
declarations: [
AppComponent,
LoginComponent,
DashboardComponent,
HomeComponent,
DialogResultExampleDialog
],
entryComponents: [DialogResultExampleDialog]
You need to use entryComponents
under @NgModule
.
This is for dynamically added components that are added using ViewContainerRef.createComponent()
. Adding them to entryComponents tells the offline template compiler to compile them and create factories for them.
The components registered in route configurations are added automatically to entryComponents
as well because router-outlet
also uses ViewContainerRef.createComponent()
to add routed components to the DOM.
So your code will be like
@NgModule({
declarations: [
AppComponent,
LoginComponent,
DashboardComponent,
HomeComponent,
DialogResultExampleDialog
],
entryComponents: [DialogResultExampleDialog]
This is happening because this is a dynamic component and you didn't add it to entryComponents
under @NgModule
.
Simply add it there:
@NgModule({
/* ----------------- */
entryComponents: [ DialogResultExampleDialog ] // <---- Add it here
Look at how the Angular team talks about entryComponents
:
entryComponents?: Array<Type<any>|any[]>
Specifies a list of components that should be compiled when this module is defined. For each component listed here, Angular will create a ComponentFactory and store it in the ComponentFactoryResolver.
Also, this is the list of the methods on @NgModule
including entryComponents
...
As you can see, all of them are optional (look at the question marks), including entryComponents
which accept an array of components:
@NgModule({
providers?: Provider[]
declarations?: Array<Type<any>|any[]>
imports?: Array<Type<any>|ModuleWithProviders|any[]>
exports?: Array<Type<any>|any[]>
entryComponents?: Array<Type<any>|any[]>
bootstrap?: Array<Type<any>|any[]>
schemas?: Array<SchemaMetadata|any[]>
id?: string
})