[转]Angular2 Material2 封装组件 —— confirmDialog确定框
本文转自: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确定框的更多相关文章
- TagHelper+Layui封装组件之Radio单选框
TagHelper+Layui封装组件之Radio单选框 标签名称:cl-radio 标签属性: asp-for:绑定的字段,必须指定 asp-items:绑定单选项 类型为:IEnumerable& ...
- Vue 爬坑之路(九)—— 用正确的姿势封装组件
迄今为止做的最大的 Vue 项目终于提交测试,天天加班的日子终于告一段落... 在开发过程中,结合 Vue 组件化的特性,开发通用组件是很基础且重要的工作 通用组件必须具备高性能.低耦合的特性 为了满 ...
- vue封装公用弹出框方法,实现点击出现操作弹出框
vue封装公用弹出框方法,实现点击出现操作弹出框 如上图所示,这次要实现一个点击出现操作弹框的效果:并将这个功能封装成一个函数,便于在项目的多个地方使用. 具体思路是: 封装一个组件,组件保护一个插槽 ...
- 原生Js封装的弹出框-弹出窗口-页面居中-多状态可选
原生Js封装的弹出框-弹出窗口-页面居中-多状态可选 实现了一下功能: 1.title可自定义 可拖拽 2.width height可以自定义 3.背景遮罩和透明度可以自定义 4.可以自己编辑弹出 ...
- 【ExtJS】关于标准模块化封装组件
在此之前,自己封装自定义控件用的是这样的方式: Ext.define('My.XXX',{ extend: 'Ext.YYY', xtype: 'ZZZ', . . . items:[ ... ] } ...
- react第七单元(组件的高级用法-组件的组合(children的用法)-高阶组件-封装组件)
第七单元(组件的高级用法-组件的组合(children的用法)-高阶组件-封装组件) #受控组件 简而言之,就是受到状态state控制的表单,表单的值改变则state值也改变,受控组件必须要搭配onc ...
- [转]angular2封装material2对话框组件
本文转自:https://www.jianshu.com/p/da9978e25566 1. 说明 angular-material2自身文档不详,控件不齐,使用上造成了很大的障碍.这里提供一个方案用 ...
- angular2.x 下拉多选框选择组件
angular2.x - 5.x 的下拉多选框选择组件 ng2 -- ng5.最近在学angular4,经常在交流群看见很多人问 下拉多选怎么做... 今天就随便写的个. 组件源码 百度云 链接: ...
- .Net Core使用视图组件(ViewComponent)封装表单文本框控件
实例程序的界面效果如下图所示: 在表单中的搜索条件有姓名,学号,成绩.他们在一行中按照水平三等分排列. 在cshtml中用html实现上述表单效果的的代码如下: <form class=&quo ...
随机推荐
- MongoDB学习记录(一) - 安装、启动与建立数据库
简要说明一个基本概念:MongoDB中的三要素:数据库(database).集合(collection)和文档(document). 文档:类似于JSON对象,由字段(field)和值(value)组 ...
- 10.18 nslookup:域名查询工具
功能说明 nslookup命令是常用的域名解析查询工具. 如果系统没有nslookup命令,则需要安装下面的软件包: yum-y inatall bind-otil9 语法格式 nslookup ...
- Codeforces Round #486 (Div. 3) E. Divisibility by 25
Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Nginx+Django搭建
本机环境介绍 虚拟机操作系统版本如下 [root@node1 ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) IP地址 ...
- hive 语法 case when 语法
' then '精选' else null end as sale_type 注意: end不能少
- Delphi使用iTools安卓模拟器
Delphi使用iTools安卓模拟器 1.去官网下载或https://pc.qq.com/detail/11/detail_22131.html 2.安装后,需要通过设置功能,设置一下分辩率,变成手 ...
- Struts2 项目 Action 查询结果异常 org.apache.struts2.json.JSONException
问题描述 今天进行一个订单管理模块的开发时遇到一个问题:查询的订单时有时会报这个异常: org.apache.struts2.json.JSONException: java.lang.Illegal ...
- 基于vue的颜色选择器vue-color-picker
项目中有用到颜色选择器的童鞋们可以看过来了 关于color-picker的jquery的插件是有蛮多,不过vue组件没有吧,反正我没有找到, 虽然element-ui里面有这个,但是你愿意为了一个小功 ...
- struts2框架学习笔记7:struts2标签
三大标签: 1.JSP:脚本,为了替代servlet,已过时 2.JSTL:标准标签库(core.format.sql.xml),还未淘汰的只有core库 3.Struts2标签库:由Struts2开 ...
- python chr()和ord()的含义和使用方法
通过help 查看相关函数的帮助文档 >>>help (chr) chr(...) chr(i) -> character Return a string of one cha ...