我想在 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/