Angular Material Dialog

By Dillon Smart · · · 0 Comments

Angular

Angular is an awesome framework, yet it does have a steep learning curve for many developers. This post is aimed at those with experience using Angular 2/4/5 who want to use the Angular Material Dialog.

This post will assume you have already installed the @angular/material library. If you have I would refer you to take a look at the following: https://material.angular.io/guide/getting-started.

Below you can find the example code used to create the Angular Material Dialog. All the code below will be the same file.

Angular Material Dialog

import {Component, Inject, Input} from '@angular/core'; 
import {MAT_DIALOG_DATA, MatDialog, MatDialogConfig, MatDialogRef} from "@angular/material";

@Component({
    moduleId: module.id,
    selector: 'base-component',
    template: `
       <h1>This is the base template</h1>
       <button (click)="openDialog()">Click Me!</button>
    `,
})

export class BaseComponent {
    openDialog(): void {
        let dialogRef = this.dialog.open(FavoriteAnimalComponent, <MatDialogConfig>{
            width: '450px',
            height: '500px'
        });

        dialogRef.afterClosed().subscribe(result => {
            alert('You said your favorite animal is: ' + result);
        });
    }
}

/**
 * The Dialog Component 
 */
@Component({
    moduleId: module.id,
    selector: 'favorite-animal',
    template: `
        <h1>Whats your favorite animal?</h1>
        <input type="text" [(ngModel)]="animal">
        <button [mat-dialog-close]="animal" cdkFocusInitial>Choose</button>
 `;
})

export class FavoriteAnimalComponent {

   public animal: string;

}

Whats next

After you have created your components, you will need to add the FavoriteAnimalComponent to your modules entryComponents array, as this should solve any ZoneAware errors that may occur.

 

AngularJavaScript

0 Comment

Was this helpful? Leave a comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WSL vs Dual Boot speed test

Updated 16th August 2022

In this experiment, I will be using WSL2 running Ubuntu 20.04 and Ubuntu 20.04 dual-booted on the same Dell XPS. Is there a noticeable difference? Follow along for the results of the WSL vs Dual Boot speed test. If you are like me and use different development environments a lot, you may find the results

List of programming languages used for Web Development

Updated 16th August 2022

There are a wide variety of programming languages that can be used for web development. Which one you choose depends on your preferences and needs. Some languages are better suited for certain tasks than others. In this post, we’ll take a look at some of the most popular languages used for web development. JavaScript PHP

Vue 3 Popup Modal Component – Create a Modal

Updated 23rd August 2022

In this post, I’m going to show you how to create a Popup Modal Component in Vue 3. This Popup Modal will use the built-in transition component. The Modal we will create looks a lot like the Modal Dialogs used by Apple in iOS. We will use Tailwind CSS for styling. What is Vue?  Vue