angular - 在 Angular Material 自定义对话框中显示组件
问题描述
我有一个可重复使用的自定义 Material UI 对话框,我想在其中显示一个不同的组件。例如,一次调用它并显示一个登录组件,另一次显示一个注册组件,这只是示例。
问题是当我将我的组件分配给正文(正文:InvitationComponent)时,结果是一个正确的对话框,但正文(或内容)是我的组件的代码。当您发送文本时,没关系,但是当我想发送一个组件以显示在对话框中间时,这是不可能的。问题是如何将组件作为对象或模板发送以在对话框中显示?邀请组件由 shome html 代码和 iput 控件和一个按钮组成,我想在我的对话框中间显示它。(例如,我们可以在 iframe 中显示的页面)提前感谢您的帮助。
<div class="title">
<h2 mat-dialog-title>{{ data.title }}</h2>
<span mat-dialog-close class="material-icons closeButton">close</span>
</div>
<hr>
<div>
<mat-dialog-content class="mat-typography">
{{ data.body }}
</mat-dialog-content>
<hr>
<div class="actionSection">
<mat-dialog-actions>
<button mat-button class="secondary" mat-dialog-close>{{ data.cancelButton }}</button>
</mat-dialog-actions>
</div>
</div>
</div>
my calling code is:
const dialogRef = this.dialogService.open(CustomDialogComponent,
{
hasBackdrop: true,
disableClose:true,
data:{
title: 'Invite User',
body: InvitationComponent,
cancelButton: 'Close' }
那是我的自定义对话框组件:
export class CustomDialogComponent implements OnInit {
constructor(@Inject(MAT_DIALOG_DATA) public data: any,
private dialogRef: MatDialogRef<CustomDialogComponent>) {
console.log(data.body);
}
ngOnInit() {
}
解决方案
您可以在通用对话框组件 html 中只有两个不同的模板,并根据调用呈现其中的任何一个。为了更好地理解,我制作了一个 stackblitz 示例:https ://stackblitz.com/edit/angular-mat-dialog-kfdktu
推荐阅读
- view - SwiftUI:如何通过给定宽度获取 HostingController 视图/内容 maxHeight?
- javascript - 自动计算所选单选按钮的总和
- java - 用不同长度的一维列表对Java中的二维数组列表进行排序
- android - 我如何在android中模拟这个方法?
- arrays - 擦除后下标超出范围
- python - 如何折叠 Google Colab Notebook 中的代码部分但保持显示结果?
- java - “函数”类的名称不符合 java 命名约定
- sql - SQL查询执行结果差异
- openlayers - Openlayers 6 - 暂停渲染
- java - 如何在tomcat中部署java项目?