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.

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

Destructuring in JavaScript

Updated 16th August 2022

Introduced in ES6, Destructuring in JavaScript is a way to unpack values from arrays, or properties from objects, into distinct variables. What is a use-case of Destructuring in JavaScript? Until ES6, if we wanted to extract data from an array we would have to do something similar to: let message = ; let greeting

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