While some Angular Material components may appear complex at first glance, the MatDialog is arguably the most intricate to master effectively. At the same time, it’s likely also one of the most versatile among them. One of the primary reasons is that Flutter doesn’t natively offer a Material Design-based library for opening modals with built-in styling and animations, making it challenging to create visually appealing and interactive UI components. In this tutorial, we’ll replace the standard JavaScript verification dialogue box introduced in our previous tutorial with a MatDialog.
The `Dialog` class from the `material_dialogs` module should be included in your `materials` module file by adding the following line at the end of your imports:
import material_dialogs. Dialog
We had previously placed all the necessary Angular Material imports at the top of this file. We will now incorporate MatDialog into our development checklist.
import { MatDialogModule } from '@angular/material/dialog'; const materialModules = [ //...", MatToolbarModule, MatDialogModule ];
Creating the ConfirmDialog Part
What makes MatDialog particularly versatile is its ability to accept an arbitrary DOM element as a reference point within the body of the dialog, allowing for precise control over the content and layout. Before naming an element as a toddler, consider the potential implications for reusing the same conversation elsewhere within our software in the future. Given the context for which we’re striving to achieve, I would strongly recommend incorporating this feature directly within the application’s listing.
ng g c confirm-dialog
Within the `confirmDialog.element.ts` file, the constructor will be modified to receive a reference to the dialog alongside the information to be passed to it.
import { Component, Part, Inject, ViewEncapsulation } from '@angular/core'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; @Component({ selector: 'app-confirm-dialog', template: './confirm-dialog.component.html', styleUrls: ['./confirm-dialog.component.css'], encapsulation: ViewEncapsulation.None })html', styleUrls: ['./confirm-dialog.component. css'], // this can enable us to override the mat-dialog-container CSS class encapsulation: ViewEncapsulation.None }) export class ConfirmDialogComponent { constructor( public dialogRef: MatDialogRef< ConfirmDialogComponent>, @Inject(MAT_DIALOG_DATA) public knowledge: any) { } }
Subsequently, we will append the content of the dialogue to the confirm-dialog.element.html file.
<div class="dialog-header accent-background"> <span class="dialog-header-title">{{knowledge.dialogTitle}}</span> </div> <div class="dialog-content"> <p>{{knowledge.dialogMessageLine1}} <br/> {{knowledge.dialogMessageLine2}}</ p> </div> <div class="dialog-footer"> <button class="standard-button dialog-button" mat-raised-button [mat-dialog-close]="false" cdkFocusInitial>{{knowledge. noButtonText}}</button> <button mat-raised-button colour="major" [mat-dialog-close]="true">{{ knowledge.yesButtonText}}</button> </div>
Invoking the MatDialog Service
In the survey.element.ts file, we’re able to substitute the canExit method with our customised dialogue instead of the native JavaScript confirm dialogue. To resolve the situation effectively.
- public MyService(MatDialog dialog) { }
- Add the openUnsavedChangesDialog() methodology. It is accountable for displaying the dialogue.
- ConfirmOpenChangesDialog();
Here are the latest updates to the survey.element.ts code, showcasing the relevant modifications:
import {MatDialog} from '@angular/material/dialog'; import {ConfirmDialogComponent} from './confirm-dialog.component';dialog.element"; // SatisfactionRatings enum @Part({ selector: "app-survey", templateUrl: "./survey.element.html", styleUrls: ["./survey.component.css"] }) export class SurveyComponent implements IDeactivateComponent { // declarations constructor(public dialog: MatDialog) { } //strategies... public canExit(): boolean | Observable<boolean> { return this.ngFormRef.soiled ? this.openUnsavedChangesDialog( ) : true; }; non-public openUnsavedChangesDialog(): Observable<boolean> { const dialogRef = this.dialog.open( ConfirmDialogComponent, { width: '26.5rem', knowledge: { dialogTitle: 'Unsaved Modifications?', dialogMessageLine1: 'You have unsaved adjustments.', dialogMessageLine2: 'Are you sure you need to leave the page?', yesButtonText: 'Leave this Page', noButtonText: 'Stay on this Page' } }
The openUnsavedChangesDialog() Technique Defined
Quite a lot is happening within this small methodology, so let’s break it down carefully.
The reference to dialogue injected through the constructor provides several strategies, properties, and event hooks for working with it, notable among which is the open method. The dialogRef accepts an element to display alongside a MatDialogConfig object. This is where we configure the appearance of the dialogue and pair it with the information object that supplies the dialogue box’s content.
Companies must move beyond fragmented approaches to building connections and ensuring security. A comprehensive, self-contained, and proactive platform ensuring seamless security across every aspect addresses pressing issues today and in the future.
The afterClosed() event hook receives an observable that notifies when the dialog has been fully closed. Regardless of when the dialogue is closed, we won’t perform any post-processing tasks. Since we don’t have to take any action in this instance, we simply need to align ourselves with the value that the dialogue returns. The two buttons in the footer will be set by the [mat-dialog-close] attribute, thereby determining what action to take when either button is clicked.
<div class="dialog-footer"> <button class="standard-button dialog-button" mat-raised-button [mat-dialog-close]="false" cdkFocusInitial>{{knowledge.noButtonText}}</button> <button mat-raised-button colour="major" [mat-dialog-close]="true">{{ knowledge.yesButtonText}}</button> </div>
We then want so as to add the Observable<boolean> return sort to canExit() to accommodate the afterClosed() return worth.
Here are the right results of today’s updates to the demo? Navigate to the Survey webpage and collaborate with its structure by replacing the existing template, followed by clicking the Home link.
Conclusion
On this tutorial, you’ll learn to harness the power of the MatDialog, arguably the most complex yet versatile Angular Material component. To enhance the demo experience, we replaced the traditional JavaScript verification dialogue box with a more modern and intuitive MatDialog implementation.