[轉]Angular2 Material2 封裝組件 —— confirmDialog肯定框

本文轉自:https://www.jianshu.com/p/0c566fc1730djavascript

環境:

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

使用Dialog封裝confirmDialog肯定框

源代碼html

來,首先來看效果圖~java

 
刪除例子
 
肯定刪除框
 
點擊肯定後返回值

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.把組件注入到服務

爲了通用,把組件注入服務,方便在其餘地方使用。這樣的話,就不用在每次使用的時候從新定義組件。node

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註冊,就能夠使用了,例子以下:git

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 = "刪除成功"; 界面即顯示刪除成功,如上面的效果圖示。github

就這樣完成了封裝confirmDialog肯定框的組件~npm

做者:LeeChingYin 連接:https://www.jianshu.com/p/0c566fc1730d 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。
相關文章
相關標籤/搜索