css - 如何以角度禁用模态的父页面
问题描述
我在 Angular 4 中有一个模式窗口可以正常工作,但是如果用户点击背景/父页面,模式就会关闭。
我找到了一些解决方案,建议在打开模式时使用背景='静态'和键盘=假,但我们的模式使用带有 BehaviorSubject 对象的本地 Dialog 类,因此使用 .next 方法打开。我也尝试使用 div config 设置这些属性,但无济于事。
因此,我正在寻找另一种解决方案,可能使用 CSS 或其他可以直接应用于父页面或模态 HTML 的设置/属性。
请参阅下面的一些相关代码。
对话框.component.ts:
constructor(private location: PlatformLocation,
private _dialog: DialogService,
private router: Router) { }
open() {
this.showDialog = true;
const body = document.body;
body.classList.add('cell-modal-open');
}
close() {
this.dialog = undefined;
}
private handleDialog(d: Dialog) {
if (!d) {
this.close();
} else if (d.template) {
if (this.showDialog) {
this.close();
}
this.dialog = d;
this.open();
}
}
ngOnInit() {
this.subscription = this
._dialog
.getDialog()
.subscribe({
next: (d) => { this.handleDialog(d); console.log('subscribed dialog') },
error: (err) => this.handleDialogError(err)
});
this.initialiseRoutingEventListeners();
}
对话服务.ts
private d: Dialog = { template: null, size: DialogSizeEnum.XLarge };
private dialogSubject = new BehaviorSubject<Dialog>({ template: null, size: DialogSizeEnum.XLarge });
constructor() { }
showDialog(template: TemplateRef<any>, size = DialogSizeEnum.XLarge, requiresAction = false) {
Object.assign(this.d, { template: template, size: size, requiresAction: requiresAction });
if (this.d !== null) {
this.dialogSubject.next(this.d);
}
}
getDialog(): BehaviorSubject<Dialog> {
return this.dialogSubject;
}
clear() {
this.dialogSubject.next(null);
}
欢迎任何建议的方法!
解决方案
为 close() 方法添加了标志,并添加了条件以仅在 true 时设置为未定义(即来自有效位置)。
推荐阅读
- angular - 在 chart.js 中为每个标签设置特定颜色
- asp.net - 是否需要回收应用程序池才能刷新缓存的查询计划?
- javascript - 使用 VBA 搜索网站
- python-3.x - 在其他系统中运行的基于 Python 的转换后的 exe
- javascript - Promise 返回一个函数而不是一个对象
- javascript - 如何获取上一年的月份名称?
- zabbix - Zabbix 添加主机
- html - w3-include-html 导致来自 https://validator.w3.org/ 的验证错误
- android - 在我需要使用 jUnit 或 Roboelectric 测试的代码中使用 MyApplication.context
- ruby - 如何通过ruby在csv的一个单元格中分隔两个数据