angular - 单击按钮时仅打开一个模式
问题描述
我在 Angular 7 StackBlitz 示例中创建了一个模态服务:
模态
export class Modal {
protected modal: any = null;
close() {
this.modal.close();
}
}
模态服务
import { ApplicationRef, ComponentFactoryResolver, EmbeddedViewRef, Injectable, Injector } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ModalService {
private componentRef: any;
private modalContainer: any;
constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private appRef: ApplicationRef,
private injector: Injector) { }
private createFormModal(component: any): Element {
this.componentRef = this.componentFactoryResolver.resolveComponentFactory(component.component).create(this.injector);
this.componentRef.instance.modal = this;
this.appRef.attachView(this.componentRef.hostView);
return (this.componentRef.hostView as EmbeddedViewRef<any>).rootNodes[0] as HTMLElement;
}
open(component: any): void {
const alertElement = this.createFormModal(component);
const content = document.createElement('div');
content.classList.add('modal');
content.appendChild(alertElement);
this.modalContainer = document.createElement('div');
this.modalContainer.classList.add('modal');
this.modalContainer.appendChild(content);
document.body.appendChild(this.modalContainer);
}
close(): void {
this.appRef.detachView(this.componentRef.hostView);
this.modalContainer.parentNode.removeChild(this.modalContainer);
this.componentRef.destroy();
}
}
单击按钮时,我可以打开模式...
问题是我可以打开多个。
单击按钮时,如何确保仅打开一个模式?
解决方案
由于服务是单例的(整个应用程序生命中的一个实例),您可以像这样在服务中保持模式的状态:
import { ApplicationRef, ComponentFactoryResolver, EmbeddedViewRef, Injectable, Injector } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ModalService {
private componentRef: any;
private modalContainer: any;
private alertElement: any;
constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private appRef: ApplicationRef,
private injector: Injector) { }
private createFormModal(component: any): Element {
this.componentRef = this.componentFactoryResolver.resolveComponentFactory(component.component).create(this.injector);
this.componentRef.instance.modal = this;
this.appRef.attachView(this.componentRef.hostView);
return (this.componentRef.hostView as EmbeddedViewRef<any>).rootNodes[0] as HTMLElement;
}
open(component: any): void {
if(this.alertElement) return;
this.alertElement = this.createFormModal(component);
const content = document.createElement('div');
content.classList.add('modal');
content.appendChild(this.alertElement);
this.modalContainer = document.createElement('div');
this.modalContainer.classList.add('modal');
this.modalContainer.appendChild(content);
document.body.appendChild(this.modalContainer);
}
close(): void {
this.appRef.detachView(this.componentRef.hostView);
this.modalContainer.parentNode.removeChild(this.modalContainer);
this.componentRef.destroy();
this.alertElement = null;
}
}
推荐阅读
- batch-file - 批处理文件复制文件但排除某些文件
- docker-compose - 如何在容器故障时使用 docker-compose 重新启动 docker 容器
- php - Laravel Http Facade 不读取正文参数
- django - 如何提高 Django-PostgreSQL 搜索性能并提高 HTML 渲染或 AJAX 响应速度?
- ruby - 流浪机器无法从本地机器获取
- metpy - Metpy 和 dewpoint_rh
- javascript - 未处理的承诺拒绝警告 DiscordApiError:未知通道
- c++ - 如何在其他类头文件中包含类头文件
- r - bs() 输出时间序列解释
- r - 使用 sjPlot 更改系数图中的轴大小