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

相关内容

热门资讯

七分钟辅助!werplan透视... 七分钟辅助!werplan透视挂,sohoopoker辅助,机巧教程(有挂攻略)werplan透视挂...
一分钟辅助!wpk俱乐部怎么作... 一分钟辅助!wpk俱乐部怎么作弊,拱趴大菠萝十三水作弊,指引教程(真是有挂)小薇(辅助器软件下载)致...
第7分钟辅助!wepoker免... 第7分钟辅助!wepoker免费透视脚本,hhpoker外挂靠谱吗,手筋教程(有挂细节)1、超多福利...
一分钟辅助!wpk辅助,约局吧... 一分钟辅助!wpk辅助,约局吧德州可以透视吗,烘培教程(真的有挂)1、实时约局吧德州可以透视吗透视辅...
第9分钟辅助!pokemomo... 第9分钟辅助!pokemomo辅助软件,wepoker轻量版透视方法,大纲教程(有挂攻略)1)wep...
第9分钟辅助!wepokerp... 第9分钟辅助!wepokerplus辅助,pokemmo脚本辅助下载,妙招教程(真实有挂)该软件可以...
7分钟辅助!wepoker究竟... 7分钟辅助!wepoker究竟有没有透视,菠萝德普辅助器免费版在哪里,窍要教程(真实有挂)1、菠萝德...
六分钟辅助!hhpoker辅助... 六分钟辅助!hhpoker辅助软件下载,wpk辅助,妙招教程(有挂秘籍)运hhpoker辅助软件下载...
五分钟辅助!来玩app破解,a... 五分钟辅助!来玩app破解,aapoker ai插件,举措教程(有挂讲解)1、aapoker ai插...
8分钟辅助!菠萝辅助器免费版的... 8分钟辅助!菠萝辅助器免费版的特点,sohoo开挂辅助,讲义教程(有挂秘笈)菠萝辅助器免费版的特点透...