angular - Angular 中 MatDiaLog 的关闭按钮出现错误 Cannot read property 'close' of null

标签 angular angular-material dialog

我想在 Angular 6 中创建一个具有多个内容项目的组件。可能是

这是我从app.component.html传递的内容:

<popup>
  <button buttonTrigger mat-button><span >Open the popup!</span></button>
  <my-dialog-content content></my-dialog-content>
</popup>

这是popup.component.html的内容

<div (click)="openDialog()"><ng-content select="[buttonTrigger]" ></ng-content></div>
<ng-template #dialogContent>
  <ng-content select="[content]"></ng-content>
</ng-template>

这是对话框内容:

<mat-dialog-content>
my-dialog-content works!
</mat-dialog-content>

<mat-dialog-actions align="end">
    <ng-container ngProjectAs="btn-close"><button mat-button color="warn" matDialogClose>Close</button></ng-container>
    <button mat-button color="primary" type="submit" cdkFocusInitial>Submit</button>
</mat-dialog-actions>

除了关闭按钮之外,这似乎有效:

ERROR TypeError: Cannot read property 'close' of null

这是我的源代码的链接:https://stackblitz.com/edit/multiple-ngcontent

我不知道是否有解决方案或好主意来解决这个问题。

谢谢!

最佳答案

您可以使用dialog.closeAll(),以下是您必须在代码中进行的更改

这是一个例子

my-dialog-content.component.html

<mat-dialog-content>
    my-dialog-content works!
</mat-dialog-content>

<mat-dialog-actions align="end">
    <ng-container ngProjectAs="btn-close">
    <button mat-button color="warn" (click)="dialog.closeAll()">Close</button>
  </ng-container>
    <button mat-button color="primary" type="submit" cdkFocusInitial>Submit</button>
</mat-dialog-actions>

my-dialog-content.component.ts

import {MatDialog} from '@angular/material';

export class MyDialogContentComponent implements OnInit {

  constructor(public dialog: MatDialog) { }

} 

已更新Stackblitz

关于angular - Angular 中 MatDiaLog 的关闭按钮出现错误 Cannot read property 'close' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52490243/

相关文章:

angular - 使用 Observable<void> 或 Observable<any> 发出 `null` 值?

node.js - Node/Express 和部署的 Angular 中的路由不起作用

选择的 Angular 4 在 *ngFor 选项中不起作用

Android:进度对话框微调器不旋转

android - 使用自定义对话框匹配密码

Angular - 构建后未处理的导航错误

sass - 访问 Angular-Material SCSS 变量 (Angular4)

angular - Material Angular Table 'child' 生成列的组件

javascript - 在 Angular 垫表中根据 rowIndex 和 columnIndex 插入数据

JavaFX 对话框通信