本文转自:https://www.jianshu.com/p/0c566fc1730d

环境:

Angular 4.0.0
Angular2 Material2 2.0.0-beta.3
node v7.4.0
npm 4.0.5

使用Dialog封装confirmDialog确定框

源代码

来,首先来看效果图~

 
删除例子
 
确定删除框
 
点击确定后返回值

1.定义通用确定框组件

confirmDialog.component.html
<div class="clearfix">
<h1 class="pull-left" md-dialog-title>{{ config.title || '确认操作' }}</h1>
<span class="icon-close-mid pull-right md-dialog-title-close" (click)="mdDialogRef.close(false)"></span>
</div>
<div md-dialog-content>{{ config.content }}</div>
<div md-dialog-actions class="confirm-dialog-operate">
<button md-raised-button color="primary" (click)="mdDialogRef.close(true)">{{ config.button || '确定' }}</button>
<button md-raised-button (click)="mdDialogRef.close(false)" class="confirm-dialog-cancel">取消</button>
</div>
配置项 描述
config.title 可配置,默认为“ 确定操作 ”。确定框的标题。
config.content 需配置。确定框的提示内容。
config.button 可配置,默认为“ 确定 ”。操作按钮的文字。
confirmDialog.component.ts
import { Component, OnInit, Inject } from '@angular/core';
import { MdDialogRef } from '@angular/material';
import {MD_DIALOG_DATA} from '@angular/material'; @Component({
selector: 'confirm-dialog',
styleUrls: ['confirmDialog.component.scss'],
templateUrl: 'confirmDialog.component.html'
}) export class ConfirmDialogComponent implements OnInit {
config : {}; constructor(private mdDialogRef : MdDialogRef<ConfirmDialogComponent>, @Inject(MD_DIALOG_DATA) data: any){
this.config = data;
}
public ngOnInit() { }
}
confirmDialog.component.scss
.md-dialog-title-close:hover{
cursor: pointer;
}
.confirm-dialog-operate{
margin-bottom: 0;
margin-top: 15px;
align-items: center;
justify-content: center;
}
.confirm-dialog-cancel{
margin-left: 20px;
}

2.在app.module.ts引入组件

import { ConfirmDialogComponent } from './confirmDialog/confirmDialog.component';

@NgModule({
declarations: [
···
ConfirmDialogComponent,
···
],
entryComponents: [
···
ConfirmDialogComponent,
···
]
})
export class AppModule { }

3.把组件注入到服务

为了通用,把组件注入服务,方便在其他地方使用。这样的话,就不用在每次使用的时候重新定义组件。

confirmDialog.service.ts
import { Component, Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';
import { MdDialog, MdDialogRef, MdDialogConfig } from '@angular/material';
import { ConfirmDialogComponent } from './confirmDialog.component';
import { Observable } from 'rxjs/Observable'; export class confirmDialogService { constructor( @Inject(MdDialog) public _confirmDialog: MdDialog, @Inject(DOCUMENT) doc: any) {
// 打开dialog,body添加no-scroll样式
_confirmDialog.afterOpen.subscribe((ref: MdDialogRef<any>) => {
if (!doc.body.classList.contains('no-scroll')) {
doc.body.classList.add('no-scroll');
}
});
// 关闭dialog,body移除no-scroll样式
_confirmDialog.afterAllClosed.subscribe(() => {
doc.body.classList.remove('no-scroll');
});
} public confirm(contentOrConfig: any, title?: string): Observable<any> {
// 设置dialog的属性,宽度,高度,内容等。
let config = new MdDialogConfig();
config = {
width: '300px',
height: '200px'
};
if (contentOrConfig instanceof Object) {
config.data = contentOrConfig;
} else if ((typeof contentOrConfig) === 'string') {
config.data = {
content: contentOrConfig,
title: title
}
}
return this._confirmDialog.open(ConfirmDialogComponent, config).afterClosed();
}
}

4.使用例子

在需要使用的组件里的provides注册,就可以使用了,例子如下:

confirmDialog-example.component.ts
import { Component, OnInit, ViewEncapsulation, Inject } from '@angular/core';
import { confirmDialogService } from './confirmDialog.service'; @Component({
selector: 'confirmDialog',
templateUrl: 'confirmDialog-example.component.html',
providers: [confirmDialogService]
}) export class DialogExampleComponent implements OnInit {
lastCloseResult: any; public constructor(public _confirmDialogService: confirmDialogService) { } public confirm() {
this._confirmDialogService.confirm({ content: '确认删除吗?' }).subscribe(res => { // 返回结果
if (res) {
this.lastCloseResult = "删除成功";
} else {
return;
} });
} public ngOnInit() { } }
confirmDialog-example.component.html
<p>Last close result: {{lastCloseResult}}</p>

<button md-raised-button (click)="confirm()">删除</button>

确定删除后,返回结果: this.lastCloseResult = "删除成功"; 界面即显示删除成功,如上面的效果图示。

就这样完成了封装confirmDialog确定框的组件~

作者:LeeChingYin
链接:https://www.jianshu.com/p/0c566fc1730d
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

[转]Angular2 Material2 封装组件 —— confirmDialog确定框的更多相关文章

  1. TagHelper+Layui封装组件之Radio单选框

    TagHelper+Layui封装组件之Radio单选框 标签名称:cl-radio 标签属性: asp-for:绑定的字段,必须指定 asp-items:绑定单选项 类型为:IEnumerable& ...

  2. Vue 爬坑之路(九)—— 用正确的姿势封装组件

    迄今为止做的最大的 Vue 项目终于提交测试,天天加班的日子终于告一段落... 在开发过程中,结合 Vue 组件化的特性,开发通用组件是很基础且重要的工作 通用组件必须具备高性能.低耦合的特性 为了满 ...

  3. vue封装公用弹出框方法,实现点击出现操作弹出框

    vue封装公用弹出框方法,实现点击出现操作弹出框 如上图所示,这次要实现一个点击出现操作弹框的效果:并将这个功能封装成一个函数,便于在项目的多个地方使用. 具体思路是: 封装一个组件,组件保护一个插槽 ...

  4. 原生Js封装的弹出框-弹出窗口-页面居中-多状态可选

    原生Js封装的弹出框-弹出窗口-页面居中-多状态可选   实现了一下功能: 1.title可自定义 可拖拽 2.width height可以自定义 3.背景遮罩和透明度可以自定义 4.可以自己编辑弹出 ...

  5. 【ExtJS】关于标准模块化封装组件

    在此之前,自己封装自定义控件用的是这样的方式: Ext.define('My.XXX',{ extend: 'Ext.YYY', xtype: 'ZZZ', . . . items:[ ... ] } ...

  6. react第七单元(组件的高级用法-组件的组合(children的用法)-高阶组件-封装组件)

    第七单元(组件的高级用法-组件的组合(children的用法)-高阶组件-封装组件) #受控组件 简而言之,就是受到状态state控制的表单,表单的值改变则state值也改变,受控组件必须要搭配onc ...

  7. [转]angular2封装material2对话框组件

    本文转自:https://www.jianshu.com/p/da9978e25566 1. 说明 angular-material2自身文档不详,控件不齐,使用上造成了很大的障碍.这里提供一个方案用 ...

  8. angular2.x 下拉多选框选择组件

    angular2.x - 5.x 的下拉多选框选择组件 ng2 -- ng5.最近在学angular4,经常在交流群看见很多人问 下拉多选怎么做... 今天就随便写的个. 组件源码 百度云   链接: ...

  9. .Net Core使用视图组件(ViewComponent)封装表单文本框控件

    实例程序的界面效果如下图所示: 在表单中的搜索条件有姓名,学号,成绩.他们在一行中按照水平三等分排列. 在cshtml中用html实现上述表单效果的的代码如下: <form class=&quo ...

随机推荐

  1. Collision (hdu-5114

    题意:你有一个以(0, 0), (x, 0), (0, y), (x, y)为边界点的四边形,给你两个点从(x1, y1), (x2, y2)的点发射,以(1, 1)的速度走,碰到边界会反射,问你最终 ...

  2. ubuntu 应用添加进环境变量

    BG:公司同事使用的电脑系统大多为windows ,有部分mac和Ubuntu(我就是那个部分Ubuntu),某些情况为了统一格式,便下载了一些解压即可使用的软件,但是每次点开文件夹然后点开程序很繁琐 ...

  3. Rabbit mq 简单应用

    参考:http://rabbitmq.mr-ping.com/AMQP/AMQP_0-9-1_Model_Explained.html 简答模式(exchange不工作) import pika # ...

  4. Xcode8.0 / OS X EI Capitan 10.11.6 提交报错90111

    改用新系统和新版xcode(都是正式版)后,提交App Store审核时报错: INFO ITMS-90111: "Beta Toolchain. 构建新的 App 和App 更新时,必须使 ...

  5. ubuntu16.04下使用navicat连接docker mysql5.7.20

    摘要: 本文将介绍如何使用docker创建mysql容器,并使用navicat连接该mysql服务,最后提供一个navicat中文乱码问题的解决方案. docker的安装和使用在这里不再赘述,如果不是 ...

  6. SSD磁盘测试不达标排查

     最近购买了一块4T的Inter_SSD_D3-4510硬盘安装在了一台DELL PowerEdge R640服务器,经过测试发现磁盘和产品手册上描述的性能相差过大,相当于产品手册性能的1/2,一下是 ...

  7. jdk8新特性(详解)

    最近在复习外加看点面试题,jdk8的新特性虽然在项目用用到过一两个,准备系统的了解一下jdk8的常用新特性 一:Lambd表达式 也可称为闭包         引入函数式编程到Java中 为了使现有函 ...

  8. extern的使用详解(多文件编程)——C语言

    extern——关键字 extern是C语言中的一个关键字,一般用在变量名前或函数名前,作用是用来说明“此变量/函数是在别处定义的,要在此处引用”,extern这个关键字大部分读者应该是在变量的存储类 ...

  9. 顺藤摸瓜:一个专黑建筑行业的QQ黏虫团伙现形记

    QQ粘虫是已经流行多年的盗号木马,它会伪装QQ登陆界面,诱骗受害者在钓鱼窗口提交账号密码.近期,360QVM引擎团队发现一支专门攻击建筑行业人群的QQ粘虫变种,它伪装为招标文档,专门在一些建筑/房产行 ...

  10. 关于 SQL 注入的问题

    拼串 (Statement)方式 1.编译次数多,效率比较低:会出现SQL注入问题(数据安全问题):先传参数再编译. 2.Sql文对应的字符串不一样,需要再次编译.Sql文对应的字符串一样,不会再编译 ...