本文转自:https://www.techiediaries.com/angular-material-dialogs/

In this tutorial, we're going to learn how to use the Angular Material Dialog component to build a custom dialog example.

We'll also see common cases to work with the Angular Material Dialog such as:

  • how to create a dialog,
  • how to pass data to the dialog component,
  • how to get data back from a dialog component,
  • how to use various configuration options for dialogs.

Before starting, first, you’ll need to make sure you have followed the steps to setup Angular Material for your Angular application.

Most Angular Material components have their own module so you can use a specific component without importing the whole library. For Dialog you need to import the MatDialogModule module:

import {MatDialogModule} from "@angular/material";

Next you need to add the module to your main module imports array.

/* ... */

@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, MatDialogModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}

Now you are ready to create your dialog. The process involves a few steps:

  • first, you need to import and inject MatDialog in the component constructor where you want to call the dialog,
  • next, you need to create an instance of MatDialogConfig which holds configuration options for the dialog (this is optional, you can also pass a literal object i.e {...}),
  • finally you need to call the open() method of the injected MatDialog instance with the component to use as the body of the dialog and the configuration object.

From the final step, you can understand that the open() method needs a component as a body so you'll need to create an Angular component.

Use the Angular CLI to generate a component:

ng g component dialog-body

Next, you’ll need to import the dialog component in the app module and add it into the declarations and entryComponents arrays:

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { MatDialogModule } from "@angular/material"; import { AppComponent } from "./app.component";
import { DialogBodyComponent } from "./dialog-body/dialog-body"; @NgModule({
declarations: [AppComponent, DialogBodyComponent],
imports: [BrowserModule, MatDialogModule],
providers: [],
bootstrap: [AppComponent],
entryComponents: [DialogBodyComponent]
})
export class AppModule {}

Step 1: Importing and Injecting MatDialog

To be able to call the dialog, you'll need to import and inject MatDialog in the calling component i.e for this example AppComponent:

import { Component, Inject } from "@angular/core";
import {
MatDialog,
MatDialogConfig
} from "@angular/material"; @Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
title = "Example Angular Material Dialog"; constructor(private dialog: MatDialog) {}
}

Step 2: Opening the Dialog

Next add the openDialog() method which opens the dialog:

  openDialog() {
const dialogConfig = new MatDialogConfig();
this.dialog.open(DialogBodyComponent, dialogConfig);
}

As we already said, you open the dialog by calling the open() method of the injected MdDialog instance and you pass the dialog component as a parameter and an optional configuration object.

You can pass different configuration option such as:

  • data: object or string to send data to the dialog,
  • height: set the height of the dialog,
  • width: set the width of the dialog,
  • disableClose: enable/disable closing the form by clicking outside the dialog,
  • autoFocus: if true, automatically sets focus on the first form field etc.

Step 3: Creating the Dialog Component

We have previously generated the dialog body component. Now let's use Angular Material directives designed to style the dialog body:

  • mat-dialog-title: This directive is used for the title of the dialog,
  • mat-dialog-content: this directive is designed for the container of body of this dialog,
  • mat-dialog-actions: this directive is designed for the container of the action buttons at the bottom of the dialog

Open src/dialog-body/dialog-body.htmland add:

<h2 mat-dialog-title>This is a Dialog title</h2>

<mat-dialog-content>

<p> Place content here </p>
</mat-dialog-content> <mat-dialog-actions>
<button class="mat-raised-button" (click)="close()">Close</button>
</mat-dialog-actions>

Step 4: Closing the Dialog and Implementing Action Buttons

The MatDialogRef provides a reference of the opened dialog. This reference can be used to close the dialog and also to notify the calling component when the dialog gets closed. Any component created via MatDialog can inject the MatDialogRef reference and use it to perform the previously mentionned operations.

Now let's implement the close button. First import MatDialogRef from @angular/material:

import { MatDialogRef } from "@angular/material";

Next inject MatDialogRef<DialogBodyComponent> as dialogRef.

@Component({
selector: "dialog-b",
template: "<h1>Dialog body component</h1>"
})
export class DialogBodyComponent {
constructor(
public dialogRef: MatDialogRef<DialogBodyComponent>){}

Finally, you can use this reference to the dialog component to control many aspects such as closing the dialog and sending data back to the parent component etc.


close() {
this.dialogRef.close();
}

You can optionally pass some value which can be received back in the calling component.

    close() {
this.dialogRef.close("Thanks for using me!");
}

Step 5: Sending Data to The Dialog Component

To be able to send or more accurately share data with the dialog component, you can use the data option to pass data:

  openDialog() {
const dialogConfig = new MatDialogConfig();
dialogConfig.data = "some data";
let dialogRef = this.dialog.open(DialogBodyComponent, dialogConfig);
}

You can also pass objects instead of simple string values:

dialogConfig.data = { name: "some name"};

For accessing shared data in your dialog component, you need to use the MAT_DIALOG_DATAinjection token:

import { Component, Inject } from "@angular/core";

import {
MAT_DIALOG_DATA
} from "@angular/material"; @Component({
selector: "dialog-b",
template: "passed in data: "
})
export class DialogBodyComponent {
constructor(@Inject(MAT_DIALOG_DATA) public data: any) {}
}

Step 6: Receiving Data from The Dialog Component

In the calling component, we can get the data passed from the dialog. From the previous example, you can see that calling the open() method returns a reference to the dialog:

 let dialogRef = this.dialog.open(DialogBodyComponent, dialogConfig);

This dialog reference has a afterClosed() observable which can be subscribed to. The data passed from the dialog is emitted from this observable.

dialogRef.afterClosed().subscribe(value => {
console.log(`Dialog sent: ${vaue}`);
});

Conclusion

Dialogs represents an important UI component for most web application and thanks to Angular Material you can quickly craft professional and good looking dialogs without having to deal with CSS.

10 Apr 2018

AhmedFollow Django developer

[转]Angular 4|5 Material Dialog with Example的更多相关文章

  1. [转]Angular4 引用 material dialog时自定义对话框/deep/.mat-dialog-container

    本文转自:https://blog.csdn.net/qq_24078843/article/details/78560556 版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...

  2. 【转】Android新组件Material Dialog,SwipeRefreshLayout,ListPopupWindow,PopupMenu等

    朝花夕拾----新组件的学习和使用 分类: Android UI2015-06-26 11:31 440人阅读 评论(0) 收藏 举报 uidialogMaterial   目录(?)[-] Mate ...

  3. angular material dialog应用

    1. 打开弹窗的点击事件 project.component.html <button mat-icon-button class="action-button" (clic ...

  4. 拟物设计和Angular的实现 - Material Design(持续更新)

    Material Design是Google最新发布的跨平台统一视觉设计语言.直接翻译是物质设计,但是我更倾向于使用"拟物设计"更为准确. 据谷歌介绍,Material Desig ...

  5. Angular 学习笔记 (Material Datepicker)

    https://material.angular.io/components/datepicker/overview 官网介绍很清楚了,这里记入一下我比较不熟悉的. 1. moment js Angu ...

  6. Angular 学习笔记 Material

    以后都不会写 0 到 1 的学习记入了,因为官网已经写得很好了. 这里只写一些遇到的坑或则概念和需要注意的事情. Material Table 1. ng-content 无法传递 CdkColumn ...

  7. 拟物设计和Angular的实现 - Material Design

    Material Design是Google最新发布的跨平台统一视觉设计语言.直接翻译是物质设计,但是我更倾向于使用"拟物设计"更为准确. 据谷歌介绍,Material Desig ...

  8. Angular 学习笔记 (Material Select and AutoComplete)

    记入一些思考 : 这 2 个组件有点像,经常会搞混. select 的定位是选择. 目前 select 最糟糕的一点是 not search friendly. 还有当需要 multiple sele ...

  9. Angular 学习笔记 (Material table sticky 原理)

    更新 : 2019-12-03 今天踩坑了, sticky 了解不够深 refer http://www.ruanyifeng.com/blog/2019/11/css-position.html 阮 ...

随机推荐

  1. 第35章:MongoDB-集群--Master Slave(主从复制)

    ①主从复制 最基本的设置方式就是建立一个主节点和一个或多个从节点,每个从节点要知道主节点的地址.采用双机备份后主节点挂掉了后从节点可以接替主机继续服务,所以这种模式比单节点的高可用性要好很多. ②注意 ...

  2. pop

    package com.example.hellopopupwindow; import android.os.Bundle; import android.app.Activity; import ...

  3. Oracle服务器和客户端安装在同一台机器的情况

    最近重装了系统,所有的开发环境需要重新部署一下,因此重新安装了Oracle,结果原来没有问题,这一次又碰到了几个问题(tns12154和tns03505),让我好一搞啊.不过又重新对Oracle加深了 ...

  4. 定时任务 Wpf.Quartz.Demo.5 (升级版)

    老规矩:先把全部源码上传,见本文底部. 相对于Demo3的区别,就是能自动加载继承了IJob的任务,任务主体程序分离. 在exe执行文件的同级下建一个MyJobs的文件夹,每次会自动扫描该文件夹下的J ...

  5. 札记:Property动画

    简介 Android 3.0 (API level 11)引入了属性动画系统,它是一个完善的框架,可以用来对几乎任何对象进行动画.只需要指定要动画的对象属性,动画时长,属性值区间等,无论对像是否在屏幕 ...

  6. 项目Alpha冲刺(团队3/10)

    项目Alpha冲刺(团队3/10) 团队名称: 云打印 作业要求: 项目Alpha冲刺(团队) 作业目标: 完成项目Alpha版本 团队队员 队员学号 队员姓名 个人博客地址 备注 221600412 ...

  7. MySQL slow_log表不能修改成innodb引擎

    背景 从mysql.slow_log 获取慢查询日志很慢,该表是csv表,没有索引. 想添加索引来加速访问,而csv引擎不能添加索引(csv引擎存储是以逗号分割的文本来存储的),只能改存储引擎来添加索 ...

  8. 源码调试debug_info 的作用和使用方法

    在他通过gcc来编译程序时,在map文件中,经常会遇到如下的情况: .debug_info 0x002191b6 0x1aa9 XXX .debug_info 0x0021ac5f 0xce4 XXX ...

  9. Testing - 软件测试知识梳理 - 测试分类

    参考信息 软件测试分类 经典软件测试技术分类 软件测试方法汇总 简洁分类 对软件内部结构的深入程度 黑盒测试:又叫功能测试.数据驱动测试或基于需求规格说明书的功能测试. 该测试类别注重于测试软件的功能 ...

  10. Spring Boot - Profile配置

    Profile是什么 Profile我也找不出合适的中文来定义,简单来说,Profile就是Spring Boot可以对不同环境或者指令来读取不同的配置文件. Profile使用 假如有开发.测试.生 ...