The `MatDialog` service can be used to open modal dialogs with Material Design styling and
animations.
A dialog is opened by calling the `open` method with a component to be loaded and an optional
config object. The `open` method will return an instance of `MatDialogRef`:
```ts
let dialogRef = dialog.open(UserProfileComponent, {
height: '400px',
width: '600px',
});
```
The `MatDialogRef` provides a handle on the opened dialog. It can be used to close the dialog and to
receive notifications when the dialog has been closed. Any notification Observables will complete when the dialog closes.
```ts
dialogRef.afterClosed().subscribe(result => {
console.log(`Dialog result: ${result}`); // Pizza!
});
dialogRef.close('Pizza!');
```
Components created via `MatDialog` can _inject_ `MatDialogRef` and use it to close the dialog
in which they are contained. When closing, an optional result value can be provided. This result
value is forwarded as the result of the `afterClosed` Observable.
```ts
import {inject} from '@angular/core';
@Component({/* ... */})
export class YourDialog {
dialogRef = inject(MatDialogRef);
closeDialog() {
this.dialogRef.close('Pizza!');
}
}
```
### Specifying global configuration defaults
Default dialog options can be specified by providing an instance of `MatDialogConfig` for
`MAT_DIALOG_DEFAULT_OPTIONS` in your app config.
```ts
bootstrapApplication(MyApp, {
providers: [
{provide: MAT_DIALOG_DEFAULT_OPTIONS, useValue: {hasBackdrop: false}}
]
});
```
> **Note:** The value provided for `MAT_DIALOG_DEFAULT_OPTIONS` **replaces** the built-in defaults
> entirely rather than merging with them. For example, providing `{disableClose: true}` means that
> all other defaults (such as `hasBackdrop`) will be `undefined`. If you only want to override
> specific properties, spread the defaults first:
>
> ```ts
> {provide: MAT_DIALOG_DEFAULT_OPTIONS, useValue: {...new MatDialogConfig(), disableClose: true}}
> ```
>
> When you call `dialog.open()` with a config, that config is merged on top of these defaults, so
> per-dialog options always take precedence.
### Sharing data with the Dialog component.
If you want to share data with your dialog, you can use the `data`
option to pass information to the dialog component.
```ts
let dialogRef = dialog.open(YourDialog, {
data: { name: 'austin' },
});
```
To access the data in your dialog component, you have to use the MAT_DIALOG_DATA injection token:
```ts
import {Component, inject} from '@angular/core';
import {MAT_DIALOG_DATA} from '@angular/material/dialog';
@Component({
selector: 'your-dialog',
template: 'passed in {{ data.name }}',
})
export class YourDialog {
data = inject<{name: string}>(MAT_DIALOG_DATA);
}
```
Note that if you're using a template dialog (one that was opened with a `TemplateRef`), the data
will be available implicitly in the template:
```html
Hello, {{data.name}}
```
### Dialog content
Several directives are available to make it easier to structure your dialog content:
| Name | Description |
|------------------------|---------------------------------------------------------------------------------------------------------------|
| `mat-dialog-title` | \[Attr] Dialog title, applied to a heading element (e.g., `
`, `
`) |
| `` | Primary scrollable content of the dialog. |
| `` | Container for action buttons at the bottom of the dialog. Button alignment can be controlled via the `align` attribute which can be set to `end` and `center`. |
| `mat-dialog-close` | \[Attr] Added to a `