[转]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 ...
随机推荐
- MFC禁用关闭按钮
有时候我们在写MFC程序时,需要在对话框中开启线程处理一些事情,如果在线程执行过程中点击关闭按钮,会导致程序崩溃. 这里介绍一种解决方法,禁用关闭按钮 解决方法 开启线程前禁用关闭按钮 CMenu* ...
- 《mysql必知必会》学习_第16章_20180807_欢
第16章:创建高级联结. P106 select concat(RTrim(vend_name),'(',RTrim(vend_country),')') as vend_title from ven ...
- MySQL与SQL语句的操作
MySQL与SQL语句的操作 Mysql比较轻量化,企业用的是Oracle,基本的是熟悉对数据库,数据表,字段,记录的更新与修改 1. mysql基本信息 特殊数据库:information_sche ...
- SDWebImage之工具类
SDWebImage使用了很多工具类来对图片的处理,比如获取图片类型.图片放大缩小.GIF图片处理.图片解压缩处理等.下面我们来看一下这几个工具类. 1.NSData+ImageContentType ...
- The MAC is invalid
在使用laravel框架进行网站开发时,我们会使用laravel的Crypt类对用户的密码进行加密来达到信息加密的目的,Crypt类会对数据加密时会依赖APP_KEY,所以当更换了APP_KEY时,再 ...
- OpenCL科普及在ubuntu 16.04 LTS上的安装
OpenCL(Open Computing Language,开放计算语言)是一个为异构平台编写程序的框架,此异构平台可由CPU.GPU.DSP.FPGA或其他类型的处理器與硬體加速器所组成.Open ...
- Win10手记-为应用集成SQLite(二)
接上篇内容,这里给大家分享我的辅助访问类,采用了异步方法,封装了常用的访问操作,一些操作还是纯CLI的. SQLiteDBManager using System; using System.Coll ...
- python 变量进阶(理解)
变量进阶(理解) 目标 变量的引用 可变和不可变类型 局部变量和全局变量 01. 变量的引用 变量 和 数据 都是保存在 内存 中的 在 Python 中 函数 的 参数传递 以及 返回值 都是靠 引 ...
- Fillder Script语法
官方的Fiddler Script使用文档 http://docs.telerik.com/fiddler/KnowledgeBase/FiddlerScript/ModifyRequestOrRes ...
- ubuntu16.04 程序开机自启动设置及启动优化
使用过程中,为了方便使用,有一些程序需要开机时自启动应用,下面将介绍一下ubuntu16.04下程序的开机自启动设置方法. 1 建立一个可执行程序的运行脚本如 keepalive.sh.内部写入要执 ...