0

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

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

打破其他对话框窗口(例如,在 MatDialog 中使用的 MatDatepicker 显示在它后面)。在本地设置似乎没有效果,根据这篇文章更改视图封装: Displaying a mat-select inside a modal dialog 仍然会破坏其他对话框窗口(与全局设置样式相同)。我可以通过其他方式实现它吗?

编辑:

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

4

2 回答 2

1

好的,好像我找到了问题的原因和解决方案。所以问题的原因是引导模式窗口(ngb-modal-window)出现在背景(ngb-modal-backdrop)下,如下所述:

Bootstrap 模态出现在背景下

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

Bootstrap 模态出现在背景下

1.) 使用NgbModalOptions禁用背景(将backdrop属性设置为 false)并为模式窗口添加类(设置windowClass属性)。然后在全局文件中z-index为我们的窗口类设置非常低的值,如下所示:style.css

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

缺点是,我们现在没有引导模式的背景。

2.) 直接在 html 层次结构中的 body 容器下添加模态窗口,或者只是将其附加到它,如下所述:

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

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

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

于 2019-06-04T17:43:23.813 回答
0

当我初始化 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>
于 2019-06-04T14:34:51.213 回答