環(huán)境:
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 || '確認(rèn)操作' }}</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>
配置項(xiàng) | 描述 |
---|---|
config.title | 可配置猎塞,默認(rèn)為“ 確定操作 ”老厌。確定框的標(biāo)題。 |
config.content | 需配置逮栅。確定框的提示內(nèi)容秕脓。 |
config.button | 可配置犬缨,默認(rèn)為“ 確定 ”遇骑。操作按鈕的文字悯姊。 |
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.把組件注入到服務(wù)
為了通用名党,把組件注入服務(wù),方便在其他地方使用挠轴。這樣的話,就不用在每次使用的時候重新定義組件耳幢。
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');
}
});
// 關(guān)閉dialog,body移除no-scroll樣式
_confirmDialog.afterAllClosed.subscribe(() => {
doc.body.classList.remove('no-scroll');
});
}
public confirm(contentOrConfig: any, title?: string): Observable<any> {
// 設(shè)置dialog的屬性睛藻,寬度启上,高度,內(nèi)容等店印。
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: '確認(rèn)刪除嗎按摘?' }).subscribe(res => {
// 返回結(jié)果
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>
確定刪除后包券,返回結(jié)果: this.lastCloseResult = "刪除成功"; 界面即顯示刪除成功,如上面的效果圖示炫贤。
就這樣完成了封裝confirmDialog確定框的組件~