Angular - 使用组件包装器和组件作为参数的材料模态框
创始人
2024-10-14 23:00:35
0

要实现“Angular - 使用组件包装器和组件作为参数的材料模态框”,你可以按照以下步骤进行操作:

  1. 首先,创建一个名为modal-wrapper的组件包装器组件。这个组件将负责显示和管理材料模态框的打开和关闭。
import { Component, ComponentFactoryResolver, OnInit, ViewChild, ViewContainerRef } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';

@Component({
  selector: 'app-modal-wrapper',
  template: `
    
`, }) export class ModalWrapperComponent implements OnInit { @ViewChild('modalContainerRef', { read: ViewContainerRef }) modalContainerRef: ViewContainerRef; constructor(private dialog: MatDialog, private componentFactoryResolver: ComponentFactoryResolver) {} ngOnInit(): void {} openModal(component: any): void { this.modalContainerRef.clear(); const componentFactory = this.componentFactoryResolver.resolveComponentFactory(component); const componentRef = this.modalContainerRef.createComponent(componentFactory); const dialogRef = this.dialog.open(componentRef.instance); dialogRef.afterClosed().subscribe(() => { componentRef.destroy(); }); } }
  1. 创建一个材料模态框组件,例如my-dialog组件,它将作为参数传递给modal-wrapper组件。
import { Component } from '@angular/core';
import { MatDialogRef } from '@angular/material/dialog';

@Component({
  selector: 'app-my-dialog',
  template: `
    

My Dialog

Hello, this is my custom dialog content.

`, }) export class MyDialogComponent { constructor(public dialogRef: MatDialogRef) {} }
  1. 在需要打开材料模态框的组件中使用modal-wrapper组件。


import { Component } from '@angular/core';
import { ModalWrapperComponent } from './modal-wrapper.component';
import { MyDialogComponent } from './my-dialog.component';

@Component({
  selector: 'app-my-component',
  template: `
    
    
  `,
})
export class MyComponent {
  constructor(private modalWrapper: ModalWrapperComponent) {}

  openDialog(): void {
    this.modalWrapper.openModal(MyDialogComponent);
  }
}

以上代码示例演示了如何创建一个可复用的组件包装器modal-wrapper,以及如何使用modal-wrapper组件来打开一个具体的材料模态框my-dialog

相关内容

热门资讯

这一问题亟待解决!新鸿狐辅助软... 这一问题亟待解决!新鸿狐辅助软件是真的吗,奇迹思维辅助(透视)app-一贯存在有挂1、让任何用户在无...
无独有偶!微信小程序微乐破解器... 无独有偶!微信小程序微乐破解器2024,蛮王大厅辅助教程(透视)攻略-切实是真的挂1、下载好微信小程...
随着!微乐家乡小程序脚本,同城... 随着!微乐家乡小程序脚本,同城游辅助工具(透视)工具-原来真的有挂1、微乐家乡小程序脚本公共底牌简单...
推出新举措!广东雀神智能插件安... 推出新举措!广东雀神智能插件安装免费,好友赣南脚本插件(透视)方法-好像是真的挂1、操作简单,无需广...
做出回应!新518互游插件下载... 做出回应!新518互游插件下载,川娱竞技辅助(透视)挂-确实存在有挂新518互游插件下载脚本下载中分...
有消息称!广东雀神智能插件免费... 有消息称!广东雀神智能插件免费,新畅游互娱辅助(透视)app-原来是真的挂该软件可以轻松地帮助玩家将...
出乎意料的是!微信小程序微乐陕... 出乎意料的是!微信小程序微乐陕西挖坑辅助,新上游拼十辅助(透视)脚本-竟然真的是有挂1、完成微信小程...
现就发布提示!大当家辅助脚本下... 现就发布提示!大当家辅助脚本下载,邯郸胡乐挂(透视)工具-果然真的有挂1、这是跨平台的大当家辅助脚本...
日前!拱趴大菠萝技巧,传送屋激... 日前!拱趴大菠萝技巧,传送屋激k有没有挂(透视)软件-本来有挂1、传送屋激k有没有挂辅助器安装包、传...
长期以来!wepoker辅助器... 长期以来!wepoker辅助器免费,越乡游辅助软件(透视)挂-竟然真的有挂1、wepoker辅助器免...