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

相关内容

热门资讯

查到实测!pokerworld... 查到实测!pokerworld软件,hh poker辅助有用吗,辅助教程(有挂技巧)1、很好的工具软...
玩家必看科普!wepoker破... 1、玩家必看科普!wepoker破解版内购,hhpoker到底可以作弊吗,揭秘攻略(有挂教程);详细...
玩家必备科技!wepoker钻... 玩家必备科技!wepoker钻石怎么看底牌,hhpoker开挂教程,玩家教程(有挂方法),支持语音通...
最新技巧!智星德州插件怎么下载... 1、最新技巧!智星德州插件怎么下载,大菠萝手游辅助,攻略教程(有挂透视)2、进入游戏-大厅左侧-新手...
科技通报!pokemmo脚本辅... 您好,pokemmo脚本辅助器下载这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302...
玩家必看教程!wepoker安... 玩家必看教程!wepoker安装教程,wepoker分析,详细教程(有挂软件);玩家必备必赢加哟《1...
一分钟了解!佛手在线大菠萝智能... 一分钟了解!佛手在线大菠萝智能辅助器,佛手在线大菠萝技巧,扑克教程(有挂辅助)是一款可以让一直输的玩...
我来教大家!wepoker透视... 我来教大家!wepoker透视功能下载,聚星ai辅助工具下载,微扑克教程(有挂透明);聚星ai辅助工...
发现一款!wepoker私人局... 此外,数据分析德州(wepoker私人局透视方法)辅助神器app还具备辅助透视行为开挂功能,通过对客...
如何分辨真伪!拱趴大菠萝辅助神... 如何分辨真伪!拱趴大菠萝辅助神器,德州局透视脚本下载安装最新版本,2025新版教程(有挂教程)准备好...