加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在Material Angular中显示简单的警报对话框

发布时间:2020-12-17 07:06:45 所属栏目:安全 来源:网络整理
导读:我正在使用Material Angular(从 Angular Material开始).该网站中的示例似乎有点过于复杂,互联网上的其他所有教程似乎都已过时或者正在使用AngularJS.如何使用标题,消息和确认/取消按钮显示简单警报(就像Android的AlertDialog一样)? 解决方法 以下是您要求的
我正在使用Material Angular(从 Angular Material开始).该网站中的示例似乎有点过于复杂,互联网上的其他所有教程似乎都已过时或者正在使用AngularJS.如何使用标题,消息和确认/取消按钮显示简单警报(就像Android的AlertDialog一样)?

解决方法

以下是您要求的简单示例:

(假设以下结构)

app/
  my-alert-dialog/
    my-alert-dialog.component.html
    my-alert-dialog.component.ts
  app.component.ts
  app.module.ts

我的警觉,dialog.component.html

<h2 matDialogTitle>Unregister?</h2>
<mat-dialog-content>
  <p>When you unregister,all your data will be removed. Continue?</p>
</mat-dialog-content>
<mat-dialog-actions align="end">
  <!--
    Note that you can pass in anything to the `matDialogClose` attribute. This also includes objects,arrays,etc.
    Just make sure that you make it a property binding with those [] brackets
    Example: <button mat-button [matDialogClose]="{'lol': true}">Cancel</button>
  -->
  <button mat-button matDialogClose="cancel" color="primary">Cancel</button>
  <button mat-button matDialogClose="confirm" color="warn">Unregister</button>
</mat-dialog-actions>

上述代码的说明:

> [matDialogTitle] / [mat-dialog-title]:指示对话框标题的指令(通常用于h2元素).
> [matDialogContent] / [mat-dialog-content] / mat-dialog-content:指示对话框内容的指令.
> [matDialogActions] / [mat-dialog-actions] / mat-dialog-actions:指示对话框操作的指令.
> [matDialogClose] / [mat-dialog-close]:指示单击此元素时应关闭对话框的指令(通常用于按钮元素).可选地,该指令可以包括一个参数(任何类型),然后可以将该参数传递给打开该对话框的组件.

我的警觉,dialog.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-alert-dialog',// Replace with your own selector
  templateUrl: './my-alert-dialog.component.html'
})
export class MyAlertDialogComponent { }

app.component.ts(编辑)

import { MatDialog } from '@angular/material';
import { MyAlertDialogComponent } from './my-alert-dialog/my-alert-dialog.component';
// ...
export class AppComponent {
  constructor(private dialog: MatDialog) { }
  unregister() {
    let dialogRef = this.dialog.open(MyAlertDialogComponent);
    dialogRef.afterClosed().subscribe(result => {
      // NOTE: The result can also be nothing if the user presses the `esc` key or clicks outside the dialog
      if (result == 'confirm') {
        console.log('Unregistered');
      }
    })
  }
}

app.module.ts(编辑)

import { MatDialogModule } from '@angular/material';
import { MyAlertDialogComponent } from './my-alert-dialog/my-alert-dialog.component';

@NgModule({
  declarations: [
    // ...
    MyAlertDialogComponent
  ],imports: [
    // ...
    MatDialogModule
  ],entryComponents: [
    // See https://material.angular.io/components/dialog/overview#configuring-dialog-content-via-code-entrycomponents-code- for more info
    MyAlertDialogComponent
  ]
})
export class AppModule { }

Demo

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读