简体   繁体   English

Angular:模态 window 渲染包含在组件内

[英]Angular: Modal window rendering contained inside component

I've nested some different Angular modules.我嵌套了一些不同的 Angular 模块。 Each one has is own service and such.每个人都有自己的服务等。 The problem is when I want to open a modal window from an embedded module: Those elements open and works fine, but inside of it own module.问题是当我想从嵌入式模块打开模态 window 时:这些元素打开并且工作正常,但在它自己的模块内部。 I've searched but I couldn't find any specific solution for open the modal window outside all components (on browser main area, like a regular modal window).我已经搜索过,但找不到任何特定的解决方案来打开所有组件之外的模态 window(在浏览器主区域,如常规模态窗口)。

A GIF showing actual results:显示实际结果的 GIF:

描述

I've tried to change the z-index (on main class, separated one, hardcoded in the html tag), disabling backdrop, and many other (nasty) tricks, but none of them seems solve this.我尝试更改 z-index(在主要 class 上,分离一个,硬编码在 html 标签中),禁用背景,以及许多其他(讨厌的)技巧,但似乎没有一个可以解决这个问题。

The html looks like this: html 看起来像这样:

<div bsModal #modalFormPlanta="bs-modal" [config]="{backdrop: false}">
<div class="modal-dialog modal-md">
    <div class="modal-content">
        <app-formulario-planta-cliente *ngIf="showFormAddPlanta" [Client]="Client" [action]="PlantaSeleccionada?.id != null" [element]="PlantaSeleccionada" (finish)="closeFormPlanta($event)"></app-formulario-planta-cliente>
    </div>
</div>

This element is at the end of the html file.该元素位于 html 文件的末尾。 Outside of all <div>在所有<div>之外

Already tried on bsModal:已经在 bsModal 上尝试过:

style="z-index: 9999 !important"
tabindex="-1"
class="modal fade"
data-backdrop="false"

And the relevant code of this component:以及这个组件的相关代码:

    @ViewChild('modalFormPlanta') modalFormPlanta: ModalDirective;
    
    public openEditForm(elem) {
        this.PlantaSeleccionada = elem;
        this.showFormAddPlanta = true;
        this.modalFormPlanta.show();
    }

    public closeFormPlanta(event) {
        if (event) {
            this.PlantaSeleccionada.Cliente = event;
            this.loadPlantas();
        }
        this.modalFormPlanta.hide();
    }

Can you suggest me any solution to open every modal window on the browser area (no inside any component).您能否建议我在浏览器区域(不在任何组件内)打开每个模态 window 的任何解决方案。

Thank you very much!非常感谢!

You can generate modal component and attach it to some root node of f.ex.您可以生成模态组件并将其附加到 f.ex 的某个根节点。 AppComponent elementRef; AppComponent elementRef; Here I made a forked a DEMO (click "customers" and then "show modal").在这里我做了一个分叉的DEMO (点击“客户”然后“显示模式”)。 It attaches Modal component from a lazy loaded modules component.它从延迟加载的模块组件中附加 Modal 组件。

First, in AppComponent grab a reference of its nativeElement, to access it later when attaching modal:首先,在 AppComponent 中获取其 nativeElement 的引用,以便稍后在附加模式时访问它:

this.nativeElement = this.elRef.nativeElement

For Modal component add needed styles, like position: absolute; top: 0; left: 0对于 Modal 组件添加所需的 styles,如position: absolute; top: 0; left: 0 position: absolute; top: 0; left: 0 position: absolute; top: 0; left: 0 Then import it into entryComponents because you will create it dynamically. position: absolute; top: 0; left: 0然后将其导入entryComponents ,因为您将动态创建它。

Then instantiate Modal and attach to host:然后实例化 Modal 并附加到主机:

  showModal() {
    this.appendComponentToBody(ModalComponent);
  }

  appendComponentToBody(component: any) {
    // 1. Create a component reference from the component
    const componentRef = this.componentFactoryResolver
      .resolveComponentFactory(component)
      .create(this.injector);

    // 2. Attach component to the appRef so that it's inside the ng component tree
    this.appRef.attachView(componentRef.hostView);

    // 3. Get DOM element from component
    const domElem = (componentRef.hostView as EmbeddedViewRef<any>)
      .rootNodes[0] as HTMLElement;

    const rootElementRef = this.injector.get(this.appRef.componentTypes[0])
      .nativeElement;
    // 4. Append DOM element to the body
    rootElementRef.appendChild(domElem);
  }

Some code from appendComponentToBody function borrowed from this repo来自appendComponentToBody function 的一些代码从这个 repo借来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM