Material Dialog In Angular 6

We already know how to work around Modal Popup in jQuery and JavaScript. Now, let's spend some time on how to use Material Modal dialog in Angular 6.
 
I assume you have Material installed in your npm and you already know how to create a project in Angular. Here, I am using Visual Studio Code for the implementation purpose.
 
So, let's create one component with one button such that when we click the button, it will open a component in a Modal Popup.
 
Use the below command and create one component.
 
ng g c PopupComponent 
 
Add Material dialog in constructor and import the same from angular/material
  1. import { Component, OnInit } from'@angular/core';  
  2. import { MatDialog } from'@angular/material';  
  3. @Component({  
  4.    selector:'app-popup-component',  
  5.    templateUrl:'./popup-component.component.html',  
  6.    styleUrls: ['./popup-component.component.css']  
  7. })  
  8. exportclassPopupComponentComponentimplementsOnInit {  
  9.    constructor(privatedialog:MatDialog) { }  
  10.    ngOnInit() {  
  11.   }  
  12. }  
Add the following code in the HTML file.
  1. <button(click)="openDialog()">Click me</button>  
Add the following code for the openDialog method in .ts file.
  1. openDialog(): void {  
  2.     constdialogRef = this.dialog.open(ModalComponent, {  
  3.         panelClass: 'detailsZoom',  
  4.         data: {  
  5.             name: 'modal',  
  6.             type: 'score',  
  7.         }  
  8.     });  
  9. }  
this.dialog.open(ModalComponent)
 
Here, modalComponent is the name of the component which will get loaded in the modal dialog popup. Create this component using ng g c component name command.
 
"panelClass" is the CSS class which is applied to the modal popup. You can have your own CSS class as well in the modal popup. For that, you just need to replace the name of the CSS file of yours.
 
In the data field, pass whatever you want to pass to your modal popup. Here, we are passing the name and type for the modal.
 
Add the below code to its .ts file for the modal component class.
  1. import { Component, Inject } from'@angular/core';  
  2. import { MatDialogRef, MAT_DIALOG_DATA } from'@angular/material';  
  3. @Component({  
  4.    selector:'app-modal',  
  5.    templateUrl:'./modal.component.html',  
  6.    styleUrls: ['./modal.component.css']  
  7. })  
  8. exportclassModalComponent {  
  9.    constructor(  
  10.    publicdialogRef:MatDialogRef<ModalComponent>,  
  11. @Inject(MAT_DIALOG_DATA) publicdata:any) {  
  12.    console.log(this.data);  
  13. }  
  14. onNoClick():void {  
  15.    this.dialogRef.close();  
  16.   }  
  17. }  
@Inject is used to hold the data which you have passed from the popup component. You can log the data and see if what you have passed is getting logged or not.
 
Add the below code in html file of modal component
  1. <p>Dialog popup works </p>  
Add the below code to close the popup
  1. <button(click)="onNoClick()">Close</button>  
Since you are loading component in modal popup at run time, you need to add that comonent in "entry".
 
app.module.ts
 
Add the below line in app.module.ts.
 
entryComponents: [ModalComponent]
 
Now, you can run your project using ng serve command and load popup component in the browser. Click on "Click me" which will invoke the openDialog method. This will load your modal component in modal dialog popup.
 
Hope this blog will give you some idea how to use Material Modal Popup in Angular.