UPDATE:
A convenient way to handle dialogs in your Angular component is by using a template reference in the HTML file and passing it to the MatDialog#open
method in the TypeScript file.
Another approach is to access the template reference in the component view and pass it to a custom method that accepts the reference.
To better understand, refer to the code snippet below where two dialog examples are demonstrated:
(Assuming the following project structure)
app/
app.component.html
app.component.ts
app.component.html
:
<button mat-button (click)="openDialogWithRef(firstDialog)">Open dialog with reference</button>
<button mat-button (click)="openOtherDialog()">Open dialog programmatically</button>
<ng-template #firstDialog>
<h2 matDialogTitle>Hello, world!</h2>
<p matDialogContent><em>Content for this dialog goes here...</em></p>
<mat-dialog-actions align="end">
<button mat-button matDialogClose>Dismiss</button>
</mat-dialog-actions>
</ng-template>
<ng-template #secondDialog>
<h2 matDialogTitle>Hello, second dialog!</h2>
<p matDialogContent>This template reference was accessed using <code>@ViewChild</code>, allowing querying of components/template references in the component view.</p>
<mat-dialog-actions align="end">
<button mat-button matDialogClose>Dismiss</button>
</mat-dialog-actions>
app.component.ts
(condensed):
import { ViewChild, TemplateRef } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
// ...
export class AppComponent {
@ViewChild('secondDialog') secondDialog: TemplateRef<any>;
constructor(private dialog: MatDialog) { }
openDialogWithRef(ref: TemplateRef<any>) {
this.dialog.open(ref);
}
openOtherDialog() {
this.dialog.open(this.secondDialog);
}
}
This streamlined method eliminates the need for creating new components solely for dialogs, as well as declaring them in the entryComponents
section of your module.
However, managing multiple dialog templates within a single component view may become cumbersome.
Original solution
For your specific requirement, consider the example provided below:
(Assuming the directory structure as detailed)
app/
my-alert-dialog/
my-alert-dialog.component.html
my-alert-dialog.component.ts
app.component.ts
app.module.ts
my-alert-dialog.component.html
<h2 matDialogTitle>Unregister?</h2>
<mat-dialog-content>
<p>Upon unregistering, all stored data will be deleted. Confirm action?</p>
</mat-dialog-content>
<mat-dialog-actions align="end">
<!--
Note that any content can be passed to `matDialogClose` including objects or arrays.
Ensure to bind them using property binding like so [matDialogClose]="{'status': 'confirm'}"
-->
<button mat-button matDialogClose="cancel" color="primary">Cancel</button>
<button mat-button matDialogClose="confirm" color="warn">Unregister</button>
</mat-dialog-actions>
Detailed explanation of the above code:
[matDialogTitle]
/ [mat-dialog-title]
: Directive used typically on an h2
element to specify the dialog's title.
[matDialogContent]
/ [mat-dialog-content]
/ mat-dialog-content
: Directive denoting the content of the dialog.
[matDialogActions]
/ [mat-dialog-actions]
/ mat-dialog-actions
: Directive indicating the dialog's actions.
[matDialogClose]
/ [mat-dialog-close]
: Directive often applied on a button
element to close the dialog when clicked. It can include a parameter (of type any
) which is then passed back to the dialog opener.
my-alert-dialog.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-alert-dialog', // Customize as needed
templateUrl: './my-alert-dialog.component.html'
})
export class MyAlertDialogComponent { }
app.component.ts
(excerpt)
import { MatDialog } from '@angular/material';
import { MyAlertDialogComponent } from './my-alert-dialog/my-alert-dialog.component';
// ...
export class AppComponent {
constructor(private dialog: MatDialog) { }
unregister() {
let dialogRef = this.dialog.open(MyAlertDialogComponent);
dialogRef.afterClosed().subscribe(result => {
if (result == 'confirm') {
console.log('Unregistered');
}
})
}
}
app.module.ts
(excerpt)
import { MatDialogModule } from '@angular/material';
import { MyAlertDialogComponent } from './my-alert-dialog/my-alert-dialog.component';
@NgModule({
declarations: [
// ...
MyAlertDialogComponent
],
imports: [
// ...
MatDialogModule
],
entryComponents: [
// Additional information at https://material.angular.io/components/dialog/overview#configuring-dialog-content-via-code-entrycomponents-code-
MyAlertDialogComponent
]
})
export class AppModule { }
Live Demo