How to use Mat-Dialog in Angular ? Last Updated : 26 Oct, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Introduction:Angular Material is a UI component library that is developed by the Angular team to build design components for desktop and mobile web applications. In order to install it, we need to have angular installed in our project, once you have it you can enter the below command and can download it. Installation syntax: javascript ng add @angular/material Approach: First we need to import 'MatDialog' from '@angular/material/dialog' and we need to create an instance for it in the constructor. Using this instance we can open the dialog box component. Now create a separate component for the dialog and write code as per the requirements. In the dialog component, we need to create an instance of 'MatDialogRef' which we should import from '@angular/material/dialog'. Import 'MatDialogModule' from '@angular/material' in app.module.ts file. Make sure that you are mentioning the Dialog component in entryComponents array in the module file. Implementation of code: app.component.html: html <button mat-raised-button (click)="openDialog()"> Pick one </button> <br> <span *ngIf="animal"> You choose: <i>{{animal}}</i> <span> app.component.ts: javascript import { Component } from '@angular/core'; import { MatDialog } from '@angular/material'; import { ExampleDialogComponent } from './example-dialog'; @Component({ selector: 'app-root', templateUrl: 'app.component.html', }) export class AppComponent { animal: string; name: string; constructor(public dialog: MatDialog) {} openDialog(): void { let dialogRef = this.dialog.open(ExampleDialogComponent, { width: '250px', data: { name: this.name, animal: this.animal } }); dialogRef.afterClosed().subscribe(result => { this.animal = result; }); } } app.module.ts: javascript import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { MatButtonModule, MatCommonModule, MatFormFieldModule, MatInputModule, } from '@angular/material'; import { AppComponent } from './example.component'; import { ExampleDialogModule } from './example-dialog'; @NgModule({ declarations: [AppComponent], exports: [AppComponent], imports: [ ExampleDialogModule, CommonModule, FormsModule, MatButtonModule, MatCommonModule, MatFormFieldModule, MatInputModule, ], }) export class AppModule {} example-dialog.component.html: html <h1 mat-dialog-title>Welcome user</h1> <div mat-dialog-content> <p>What's your favorite animal?</p> <mat-form-field> <input matInput [(ngModel)]="data.animal"> </mat-form-field> </div> <div mat-dialog-actions> <button mat-button (click)="onCancel()">No Thanks</button> <button mat-button [mat-dialog-close]="data.animal" cdkFocusInitial>Ok</button> </div> example-dialog.component.ts: javascript import { Component, Inject } from '@angular/core'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material'; @Component({ selector: 'app-example-dialog', templateUrl: 'example-dialog.component.html', }) export class ExampleDialogComponent { constructor( public dialogRef: MatDialogRef<ExampleDialogComponent>, @Inject(MAT_DIALOG_DATA) public data: any) { } onCancel(): void { this.dialogRef.close(); } } example-dialog.module.ts: javascript import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { MatButtonModule, MatCommonModule, MatDialogModule, MatFormFieldModule, MatInputModule, } from '@angular/material'; import { ExampleDialogComponent } from './example-dialog.component'; @NgModule({ declarations: [ExampleDialogComponent], entryComponents: [ExampleDialogComponent], imports: [ FormsModule, MatButtonModule, MatCommonModule, MatDialogModule, MatFormFieldModule, MatInputModule, ], }) export class ExampleDialogModule {} Output: Output when the dialog box is opened: Output when the entered value is displayed on the screen: Comment More infoAdvertise with us B bunnyram19 Follow Improve Article Tags : Web Technologies AngularJS AngularJS-Misc Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Like