angular - 如何从应用程序的任何位置打开模态

标签 angular typescript ngx-bootstrap

我需要将所有模态框放在一个地方,这样我就可以从应用程序的任何地方访问任何模态框。这可能吗?

现在,我用下面的方式打开一个模式。

openModal(template: TemplateRef<any>,cssClass:any) {
    if(this.modalRef) {
        this.closeModal();
    }
    this.config.class = cssClass;
    this.modalRef = this.modalService.show(template, this.config);
}
closeModal() {
    if(this.modalRef) {
        this.modalRef.hide();
        this.modalRef = null;
    }
}

在我的 HTML 中

<button (click)="openModal(someModal,'some-class')">Click</button>

<ng-template #someModal> <div class="modal-body"> Login Here </div></ng-template>

我试过以下方法 https://stackblitz.com/edit/angular-wuntd3

我本地的 HTML 元素 enter image description here

最佳答案

您可以创建一个自定义指令,您可以在其中从应用程序的任何位置访问模式。只有模态的内容与要求的上下文不同。

关于angular - 如何从应用程序的任何位置打开模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50267546/

相关文章:

arrays - 订阅 Angular 6

angular - 将 HTML 添加到 ngx-bootstrap 中的弹出窗口标题

angular5 - 打开两个模态时如何更改 ngx bootstrap 背景模态

javascript - 如果未定义则显示空值

javascript - 使用 debounce js 时的数据一致性

javascript - Angular 2 前端 django 2 REST 框架后端用户身份验证

typescript - 在 TypeScript 中,如何从基类返回子类?

css - 表内的时间选择器

javascript - 谓词 Angular 2 上的过滤数组

javascript - Angular : ng build fails but npm run build works fine