Back to results for “Components & Templates”
Angular › Angular Material & UI Component Libraries

How do you add a Material dialog to an Angular app?

Beginner

Answer

You inject the MatDialog service into your component, and call its open method with the component you want to display inside the dialog. The dialog handles showing itself, dimming the background, and closing correctly when the user is done.
import { MatDialog } from '@angular/material/dialog';

constructor(private dialog: MatDialog) {}

openConfirmDialog() {
  this.dialog.open(ConfirmDialogComponent);
}
Real-world example A settings page shows a confirmation dialog asking the user to confirm before deleting their account, using Angular Material's MatDialog service to handle the popup behavior correctly.

Common follow-ups: How do you pass data into a dialog component and get a result back after it closes?;How would you prevent a dialog from closing when the user clicks outside of it?