【问题标题】:Showing a component in Angular Material Custom Dialog在 Angular Material 自定义对话框中显示组件
【发布时间】:2020-10-20 10:43:56
【问题描述】:

我有一个可重复使用的自定义 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' }

那是我的 CustomDialogComponent:

export class CustomDialogComponent implements OnInit {

  constructor(@Inject(MAT_DIALOG_DATA) public data: any,
    private dialogRef: MatDialogRef<CustomDialogComponent>) {
    console.log(data.body);
  }

  ngOnInit() {
  }

【问题讨论】:

标签: angular typescript mat-dialog material-dialog


【解决方案1】:

您可以在通用对话框组件 html 中只拥有两个不同的模板,并根据调用呈现其中的任何一个。 为了更好地理解,我制作了一个 stackblitz 示例:https://stackblitz.com/edit/angular-mat-dialog-kfdktu

【讨论】:

  • 谢谢 Rehan,我说这两个只是作为一个例子,这是一个很好的解决方案,但我想说我还有一些其他的裸组件,我想将它们显示到一个自定义标题的对话框中和按钮,只是我想将我的组件注入到显示内容部分的对话框中。我不知道我能否说出我的意思。再次感谢您的回答 :) 考虑一下我有两个或更多独立的组件,我想将这些组件注入到我的自定义对话框中,并带有最喜欢的标题和按钮描述。
  • 考虑到您的要求,我已经更新了我的 stackblitz 示例。请再过一遍。如果正确,请考虑接受答案:)
  • 非常感谢 Rehan,这是可能的解决方案。我花了很多时间,但我是 Angular 的新手,对某些部分没有很好的了解。
  • 只是另一个问题,我尝试了您的代码,但我的代码有些不同,但我有一个错误:'this.vcRef' is undefined,顺便说一下我的 Angular cli 版本是 10
  • 当我在控制台中记录 'this' 时存在 vcRef 对象但是当我记录 this.vcRef 时是 'undefined'!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多