如何在对话框 window 顶部显示 mat-select 选项?

How to display mat-select options on top of dialog window?

我发现在全局css文件中设置的解决方案:

.cdk-global-overlay-wrapper, .cdk-overlay-container {
  z-index: 10000;
}

破坏其他对话框 windows(例如,在 MatDialog 中的表单内部使用的 MatDatepicker 显示在它后面)。 本地设置好像没有效果,按照这个改视图封装post: 仍然打破其他对话框 windows(与全局设置样式相同)。我可以通过其他方式实现吗?

已编辑:

stackblitz 示例,其中 select 选项甚至不显示: https://stackblitz.com/edit/angular-jkxsig-en8bze?file=app/

当我初始化 id 时它起作用了。

import {Component} from '@angular/core';

import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';

export interface Food {
  value: string;
  viewValue: string;
}

@Component({
  selector: 'ngbd-modal-basic',
  templateUrl: './modal-basic.html'
})
export class NgbdModalBasic {
    foods: Food[] = [
    {value: 'steak-0', viewValue: 'Steak'},
    {value: 'pizza-1', viewValue: 'Pizza'},
    {value: 'tacos-2', viewValue: 'Tacos'}
  ];
  closeResult: string;
  ariaId = 'modal-basic-title';

  constructor(private modalService: NgbModal) {}

  open(content) {
    this.modalService.open(content, { ariaLabelledBy: 'modal-basic-title', size: 'lg', backdrop: 'static' }, ).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  private getDismissReason(reason: any): string {
    if (reason === ModalDismissReasons.ESC) {
      return 'by pressing ESC';
    } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
      return 'by clicking on a backdrop';
    } else {
      return  `with: ${reason}`;
    }
  }
}

html

<ng-template #content let-modal let-id="ariaId">
  <div class="modal-header">
    <h4 class="modal-title" id="{{ariaId}}">Profile update</h4>
    <button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    <form>
      <div class="form-group">
        <label for="dateOfBirth">Date of birth</label>
        <div class="input-group">
          <input id="dateOfBirth" class="form-control" placeholder="yyyy-mm-dd" name="dp" ngbDatepicker #dp="ngbDatepicker">
          <div class="input-group-append">
            <button class="btn btn-outline-secondary calendar" (click)="dp.toggle()" type="button"></button>
          </div>
        </div>
      </div>
      <h4>Basic mat-select</h4>
      <mat-form-field>
        <mat-label>Favorite food</mat-label>
        <mat-select>
          <mat-option *ngFor="let food of foods" [value]="food.value">
            {{food.viewValue}}
          </mat-option>
        </mat-select>
      </mat-form-field>
    </form>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-outline-dark" (click)="modal.close('Save click')">Save</button>
  </div>
</ng-template>

<button class="btn btn-lg btn-outline-primary" (click)="open(content)">Launch demo modal</button>

<hr>

<pre>{{closeResult}}</pre>

好的,看来我找到了问题的原因和解决方案。 所以问题的原因是 bootstrap modal window (ngb-modal-window) 出现在 background (ngb-modal-backdrop),如此处所述:

Bootstrap modal appearing under background

设置模式 window z-index 没有效果,因为背景总是在堆叠上下文中更高,并且 bootstrap 框架总是在组件创建后将其 z-index 覆盖为 1050 . 这个问题有两个解决方案,我在这里找到:

Bootstrap modal appearing under background

1.) 使用 NgbModalOptions 禁用背景(将 backdrop 属性 设置为 false)并为模态添加 class window(设置 windowClass 属性)。然后为我们的 window class 设置非常低的 z-index 值,像这样,在全局 style.css 文件中:

ngb-modal-window.test {
    z-index: 1;
}

缺点是,我们现在 bootstrap 模式没有背景。

2.) 在 html 层次结构中直接在 body 容器下添加模态 window 或将其附加到它,如此处解释:

https://github.com/twbs/bootstrap/issues/23916

我还没有测试过,但它应该可以工作,在这里你可以找到如何将元素附加到 html 正文的信息:

https://hackernoon.com/angular-pro-tip-how-to-dynamically-create-components-in-body-ba200cc289e6