I thought that I could give a more complete answer to those of us who are still studying:
Unlike sample materials, I configured the dialog box to have my own component files (html, css and ts) for ease of debugging.
The main component file "x.component.ts" (dialog box invocation)
1) add an import statement
import { MatDialog} from '@angular/material';
2) add property to constructor parameters
public dialog: MatDialog
3) define the code to call the dialog box
openDialog(title: string, text: string): void { const dialogRef = this.dialog.open(DialogComponent, { width: '350px', data: {dialogTitle: title, dialogText: text} ); dialogRef.afterClosed().subscribe(result => { }); const dialogSubmitSubscription = dialogRef.componentInstance.submitClicked.subscribe(result => { dialogSubmitSubscription.unsubscribe(); });
}
Call a function from your html file using openDialog (). I refer to DialogComponent, so make sure it is imported into your module.
import { DialogComponent } from './dialog/dialog.component';
also
entryComponents: [DialogComponent]
declare this in your entryComponents array
4) add import in your dialog.component.ts file
import { Component, Output, EventEmitter, Inject, OnInit} from '@angular/core'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
5) define properties & functions
dialogTitle: string; dialogText: string; @Output() submitClicked = new EventEmitter<any>(); constructor( public dialogRef: MatDialogRef<DialogComponent>, @Inject(MAT_DIALOG_DATA) public data: DialogData) {} ngOnInit() { this.dialogTitle = this.data.dialogTitle; this.dialogText = this.data.dialogText; } saveMessage() { const data = 'Your data'; this.submitClicked.emit(data); this.dialogRef.close(); } closeDialog() { this.dialogRef.close(); }
6) and finally HTML
<h1 mat-dialog-title>{{dialogTitle}}"</h1> <div mat-dialog-content> <p>{{dialogText}}</p> </div> <div mat-dialog-actions> <button mat-button (click)="saveMessage()" >Ok</button> <button mat-button (click)="closeDialog()" cdkFocusInitial>No Thanks</button> </div>
Hope this helps.