@Component({
template : `<p>你好</p>`
})
export class AlertComponent {
constructor(){
}
}
//必须声明两处 declarations: [ AlertComponent], entryComponents : [ AlertComponent]
//注入MdDialog对象
constructor(private mdDialog : MdDialog) { }
//打开
this.mdDialog.open(AlertComponent)
@Component({
template : `<div md-dialog-title>'确认操作'</div>
<div md-dialog-content>确认执行操作?</div>
<div md-dialog-actions>
<button md-button (click)="mdDialogRef.close('ok')">确认</button>
<button md-button (click)="mdDialogRef.close('cancel')">取消</button>
</div>`
})
export class ConfirmComponent {
constructor(private mdDialogRef : MdDialogRef<DialogComponent>){ }
}
//必须声明两处 declarations: [ ConfirmComponent], entryComponents : [ ConfirmComponent]
//注入MdDialog对象
constructor(private mdDialog : MdDialog) { }
//打开
this.mdDialog.open(ConfirmComponent).subscribe(res => {
res === 'ok' && dosomething
});
export declare class MdDialogConfig {
viewContainerRef?: ViewContainerRef;
/** The ARIA role of the dialog element. */
role?: DialogRole;
/** Whether the user can use escape or clicking outside to close a modal. */
disableClose?: boolean;
/** Width of the dialog. */
width?: string;
/** Height of the dialog. */
height?: string;
/** Position overrides. */
position?: DialogPosition;
/** Data being injected into the child component. */
data?: any;
}
config : any;
constructor(private mdDialogRef : MdDialogRef<AlertComponent>){
this.config = mdDialogRef.config.data || {};
}
//alert.component.html
<div class="title" md-dialog-title>{{config?.title || '提示'}}</div>
<div class="content" md-dialog-content>{{config?.content || ''}}</div>
<div class="actions" *ngIf="!(config?.hiddenButton)" md-dialog-actions>
<button md-button (click)="mdDialogRef.close()">{{config?.button || '确认'}}</button>
</div>
//alert.component.scss
.title, .content{
text-align: center;
}
.actions{
display: flex;
justify-content: center;
}
//alert.component.ts
@Component({
selector: 'app-alert',
templateUrl: './alert.component.html',
styleUrls: ['./alert.component.scss']
})
export class AlertComponent {
config : {};
constructor(private mdDialogRef : MdDialogRef<AlertComponent>){
this.config = mdDialogRef.config.data || {};
}
}
constructor(private mdDialog : MdDialog) { }
let config = new MdDialogConfig();
config.data = {
content : '你好'
}
this.mdDialog.open(AlertComponent, config)
@Injectable()
export class CustomDialogService {
constructor(private mdDialog : MdDialog) { }
//封装confirm,直接返回订阅对象
confirm(contentOrConfig : any, title ?: string) : Observable<any>{
let config = new MdDialogConfig();
if(contentOrConfig instanceof Object){
config.data = contentOrConfig;
}else if((typeof contentOrConfig) === 'string'){
config.data = {
content : contentOrConfig,
title : title
}
}
return this.mdDialog.open(DialogComponent, config).afterClosed();
}
//同
alert(contentOrConfig : any, title ?: string) : Observable<any>{
let config = new MdDialogConfig();
if(contentOrConfig instanceof Object){
config.data = contentOrConfig;
}else if((typeof contentOrConfig) === 'string'){
config.data = {
content : contentOrConfig,
title : title
}
}
return this.mdDialog.open(AlertComponent, config).afterClosed();
}
constructor(dialog : CustomDialogService){}
this.dialog.alert('你好');
this.dialog.alert('你好','标题');
this.dialog.alert({
content : '你好',
title : '标题',
button : 'ok'
});
this.dialog.confirm('确认吗').subscribe(res => {
res === 'ok' && dosomething
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有