Angular完整项目开发10 - 信息提示框(Dialog)

一般的软件应用中都会有一些信息提示框,比如单纯提示一条信息,用户点击确定即可;又比如某些操作之前弹框让用户确认是或否。

利用Angular Material中的Dialog组件,可以制作这些提示框。目前制作了两个:提示框Hint和确认框Prompt。

先说只有一个确定按钮的信息提示框:

1. HTML模板

hint.html

<h2 mat-dialog-title style="font-family: 微软雅黑;">提示</h2>
<mat-dialog-content class="mat-typography">
  <p>
    {{message}}
  </p>
</mat-dialog-content>
<mat-dialog-actions align="end">
  <button mat-button [mat-dialog-close]="true" cdkFocusInitial>确定</button>
</mat-dialog-actions>

2. 对话框的TS类

hint.ts

import { Component, Inject } from '@angular/core';
import { MAT_DIALOG_DATA} from '@angular/material/dialog';

@Component({
  selector: 'hint',
  templateUrl: './hint.html',
})
export class Hint  {
  constructor(
    @Inject(MAT_DIALOG_DATA) public message: string
  ) { }
}

3. 在Component中调用提示框

import { Hint } from '../common/dlg/hint';

export class DeviceComponent implements OnInit {
  constructor(
    ...
    public dialog: MatDialog,
  ) { }

  this.dialog.open(Hint, {data:"请选择一条记录!"}); //打开信息提示框
}

效果:

带有Yes or No两个按钮的操作确认框(Prompt)和信息提示框基本一样,就多一个按钮, 可以为每个按钮分别设置Yes or No的属性,Yes就设成true,No就设成false.

<button mat-button [mat-dialog-close]="false">不</button>
<button mat-button [mat-dialog-close]="true" cdkFocusInitial>是的</button>

效果:

4. 使用按钮结果

有时候对于用户按了不同按钮的结果要进行处理,比如按了Yes怎么处理,按了No怎么处理。

const dialogRef = this.dialog.open(Prompt, obj);
dialogRef.afterClosed().subscribe(result => {  //Dialog的afterClosed事件,挺有用
      if(result){
        //Yes...
      }else{
        //No...
      }
});

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鹰信息技术服务部

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值