25

I'm trying to use a MatDialog and based on this example I've implemented that dialog as follows:

import {Component, Inject, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {AuthenticationService} from '../../../service/authentication.service';
import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from '@angular/material';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent {

  constructor(private router: Router, public dialog: MatDialog) { }

  openDialog(): void {
    const dialogRef = this.dialog.open(CreateGroupDialogComponent);
  }    
}

@Component({
  selector: 'app-create-group-dialog',
  template: `
    <span>Hello World!</span>
  `
})
export class CreateGroupDialogComponent {
  constructor(public dialogRef: MatDialogRef<CreateGroupDialogComponent>) {  }
}

However, even though the dialog comes up as I press the according button, what I get it this:

enter image description here

The HTML template gets not displayed and the dimensions of the modal is wrong.

I don't see what the problem is. Why is the modal not correctly drawn?

This is the generated HTML code when opening the modal. As can be seen it's empty.

enter image description here

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378

4 Answers4

58

You need to add it to entryComponents on your @NgModule

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { MatDialogModule, MatButtonModule } from '@angular/material';

import { AppRoutingModule } from './app-routing.module';

import { AppComponent } from './app.component';
import { LoginComponent } from './login/login.component';
import { HomeComponent, DialogOverviewExampleDialogComponent } from './home/home.component';


@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    HomeComponent,
    DialogOverviewExampleDialogComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AppRoutingModule,
    MatDialogModule,
    MatButtonModule
  ],
  providers: [],
  bootstrap: [AppComponent],
  entryComponents: [
    DialogOverviewExampleDialogComponent
  ]
})
export class AppModule { }

Dup of Angular2 material dialog has issues - Did you add it to @NgModule.entryComponents?

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
flamusdiu
  • 1,722
  • 2
  • 14
  • 31
  • 1
    Yeah, that solved the issue - thank you! I just saw this is actually mentioned in the [documentation](https://material.angular.io/components/dialog/overview) (I must have overlooked it there) but I don't see that in the [plunker](https://plnkr.co/edit/?p=preview) example. – Stefan Falk Nov 11 '17 at 18:39
  • 1
    FWIW, it gives you an error in the console that tells you about it when you open the dialog box and it's missing. – flamusdiu Nov 11 '17 at 18:40
  • 1
    You sir are a legend ! Worked like a charm – Victor Sep 18 '18 at 06:21
  • Thanks a lot for this explanation. Just want to add : in version 6.x.x and later, it doesn't seem to display any error about EntryComponent – Zooly Feb 26 '19 at 10:42
  • @displayname I have an other issue. when open dialog it automatic closed within 1sec. and it show top to bottom as per i click. what is the actual problem? – Priyanka C Oct 02 '19 at 05:16
  • 3
    Anyone else have this issue after upgrading to Angular 9, and their documentation saying you no longer need "entryComponents" declared? – ymerej Feb 28 '20 at 19:01
  • @ymerej using angular 10 & facing this issue. have you got any workaround/solution? – Ganesh Jul 29 '20 at 13:37
  • @Ganesh I can't remember exactly what I did, but I think it was along creating a quick example project and comparing differences. I believe it had something do with enabling ivy engine in angular 9, that got it working... – ymerej Jul 30 '20 at 14:49
  • I am getting this on ng9 and ng10 and I have Ivy enabled. I have no idea why my dialog is coming up but no angular stuff is rendered so it is empty. If I hard-code some html elements, they show but anything "angularized" is not. – crowmagnumb Aug 05 '20 at 19:26
  • But the same dialog behaves fine in other circumstances. It is only sometimes that it doesn't render and I can't figure out what causes it yet. – crowmagnumb Aug 05 '20 at 20:04
  • @crowmagnumb May I know what error you receive & is that problem occurring in production alone or in local dev also? – Ganesh Aug 18 '20 at 03:40
  • It works, thank you. I made sure to have declared the dialog component in declarations:[ ] and entryComponents: [ ]. More information here: https://material.angular.io/components/dialog/overview#configuring-dialog-content-via-entrycomponents – yaircarreno Mar 22 '21 at 13:29
  • @crowmagnumb Did you solve this? I am having the same issue.. – Jadamae77 Apr 07 '21 at 14:59
  • @Jadamae77 Oh geez, I don't know how I didn't see Ganesh 's question above. Sorry. But to answer, yes but now I don't remember anything. Soooo much was changing in that period, I was having tons of problems and changed so many things. I don't even know if I would have recognized at the time what fixed it. :( My foggy recollection is that it ended up being something stupid on my end, but that's not much help here. :( – crowmagnumb Apr 07 '21 at 20:06
  • @crownmagnumb totally understand...thanks for the response! – Jadamae77 Apr 07 '21 at 20:22
7

I had this problem and solved it through ngZone.run() method. I am using Angular 13 so those entryComponents are not needed

So I have a page that opens a dialog (seperate compoenent) on a callback function ( like a feedback form )

But the dialog doesn't render all the angular related components but rendered only Html CSS that are written in refund-popup.component.html and refund-popup.component.css

This is because the callback comes from out of angular zone. So i am just making my dialog box run inside zone.

Solution

import { NgZone} from '@angular/core'
import { MatDialog } from '@angular/material/dialog';
import { RefundPopupComponent } from 'src/app/components/refund-popup/refund-popup.component';



@Component({
selector: 'app-create-profile',
templateUrl: './create-profile.component.html',
styleUrls: ['./create-profile.component.scss'],
})

export class CreateProfileComponent {
   constructor(private zone: NgZone , private dialog: MatDialog) {}
   
   someFunction() {
       this.zone.run(() => this.dialog.open(RefundPopupComponent}))

    }     
}

I have updated before and after images , after including ngZone run method

Before ngZone run

After Adding ngZone

Shrihari
  • 95
  • 2
  • 11
  • 1
    this worked for me, but do you have any more information on why it is necessary? – ayylmao Jan 31 '23 at 16:18
  • In my case I am opening the component out of angular zone. So I am forcing the angular to run the change detection by using `ngZone.run()` – Shrihari Mar 26 '23 at 17:02
2

with the new since 9.0 ivy Compiler the entryComponents field is no longer necessary and has been deprecated.

I simply fixed this issue by restarting my ng serve.

0

UPDATE - APRIL 2021

With Ivy, the fix for this is no longer to add your component to entryComponents. I had this issue and to make it more confusing, it worked locally running in both dev and production, but didn't work in our deployed environment. It just silently failed. After banging my head against the wall all day I discovered a circular dependency that for some reason hadn't been throwing an error. Once I removed this dependency, everything works as expected.

Jadamae77
  • 828
  • 7
  • 13