Of all of the Angular Materials elements, the MatDialog simply could be the most advanced. On the identical time, it’s most likely additionally probably the most versatile of the bunch. A part of the reason being that itβs not a part a lot as a service that may be utilized to open modal dialogs with Materials Design styling and animations. On this tutorial, weβll substitute the usual JavaScript affirm dialog that we applied within theΒ Stopping Information Loss In Angular Purposes utilizing a CanDeactivate Route GuardΒ tutorial with a MatDialog:
JavaScript Affirm Dialog
Angular Affirm Dialog
Including MatDialog to the Materials Module File
Recall that we positioned all of our Angular Materials imports within theΒ srcappsharedmodules
import {MatDialogModule} from '@angular/materials/dialog'; const materialModules = [ //... MatToolbarModule, MatDialogModule ];
Creating the ConfirmDialog Part
A part of what makes MatDialog so versatile is that its open() methodology accepts a part to indicate within the physique of the dialog. You could be tempted to create the part as a baby to the one that can name it, however it could be sensible to suppose twice earlier than doing in order we might prefer to reuse the identical dialog in different places inside out software sooner or later. For that motive, I’d suggest producing it inside the app listing:
ng g c confirm-dialog
Within the confirm-dialog.part.ts file, we’ll modify the constructor to simply accept a reference to the dialog in addition to the info that we’ll move to it:
import { Part, Inject, ViewEncapsulation } from '@angular/core'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/materials/dialog'; @Part({ selector: 'app-confirm-dialog', templateUrl: './confirm-dialog.part.html', styleUrls: ['./confirm-dialog.component. css'], // this can permit us to override the mat-dialog-container CSS class encapsulation: ViewEncapsulation.None }) export class ConfirmDialogComponent { constructor( public dialogRef: MatDialogRefConfirmDialogComponent>, @Inject(MAT_DIALOG_DATA) public information: any) { } }
Subsequent, weβll add the contents of the dialog to the confirm-dialog.part.html file:
{{information.dialogMessageLine1}}
{{information.dialogMessageLine2}}p>
Invoking the MatDialog Service
Again within the survey.part.ts file, weβre able to replace the canExit() methodology to current our customized dialog as a substitute of the native JavaScript affirm dialog. There are three issues we have to do to make that occur:
- Add a constructor that accepts a MatDialog reference.
- Add the openUnsavedChangesDialog() methodology. Itβs chargeable for exhibiting the dialog.
- Invoke the openUnsavedChangesDialog() methodology from canExit().
Right here is the up to date supply code for the survey.part.ts file that reveals the related adjustments:
// imports import { MatDialog } from "@angular/materials/dialog"; import { ConfirmDialogComponent } from "../confirm-dialog/confirm-dialog.part"; // SatisfactionRatings enum @Part({ selector: "app-survey", templateUrl: "./survey.part.html", styleUrls: ["./survey.component.css"] }) export class SurveyComponent implements IDeactivateComponent { // declarations constructor(public dialog: MatDialog) { } //strategies... public canExit(): boolean | Observable { return this.ngFormRef.soiled ? this.openUnsavedChangesDialog( ) : true; }; personal openUnsavedChangesDialog(): Observable { const dialogRef = this.dialog.open( ConfirmDialogComponent, { width: '26.5rem', information: { dialogTitle: 'Unsaved Modifications', dialogMessageLine1: 'You've unsaved adjustments.', dialogMessageLine2: 'Are you certain you need to go away the web page?', yesButtonText: 'Depart this Web page', noButtonText: 'Keep on this Web page' } }); return dialogRef.afterClosed(); } }
The openUnsavedChangesDialog() Technique Defined
Thereβs so much happening on this little methodology, so letβs unpack it.
The dialog reference that we injected by way of the constructor offers quite a few strategies, properties, and occasion hooks for working with it, crucial of which being the open() methodology. It accepts the part to show in addition to a MatDialogConfig object. Thatβs the place we set the dialogβs look and move alongside the info object that populates the dialog part.
Organizations should transcend a piecemeal strategy to networking and safety. A broad, built-in, and automatic platform that secures all edges addresses challenges now and sooner or later.
The afterClosed() occasion hook receives an observable that’s notified when the dialog is completed closing. We will do no matter processing we have to do after the dialog is closed. In our case, we donβt must do something however move alongside the worth returned by the dialog. That will get set by the 2 buttons within the footer by way of the sure [mat-dialog-close] attribute:
We then want so as to add the Observable
Right hereβs the tip results of in the present dayβs updates to theΒ Stopping Information Loss In Angular Purposes utilizing a CanDeactivate Route GuardΒ demo. To check it, navigate to the Survey web page, work together with the shape in order to replace the underlying mannequin, after which click on the House hyperlink:
Conclusion
On this tutorial, we realized learn how to use the MatDialog, probably the most advanced, and but most versatile Angular Materials part. To do this, we changed the usual JavaScript affirm dialog that we applied within theΒ Stopping Information Loss In Angular Purposes utilizing a CanDeactivate Route GuardΒ demo with a MatDialog.