【问题标题】:angular/ngbmodal: backdropClass doesnt work角度/ngbmodal:背景类不起作用
【发布时间】:2020-03-15 12:56:48
【问题描述】:

我正在尝试使用 backdropClass 将 ngbmodal 对话框的背景颜色更改为蓝色。它不起作用,为什么?

我的代码是:

* 组件调用:

let options: NgbModalOptions = {
        size: 'sm',
        backdrop:'static',
        backdropClass: "light-blue-backdrop"
      };

      var res = this.modalService.open(content, options);

*html 组件:

<ng-template #content let-modal>
<div class="modal-header">
    <h4 class="modal-title">Modal title</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">
    <p>One fine body&hellip;</p>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-light" (click)="modal.close('Close click')">Close</button>
</div>
</ng-template>

* css 类:

.dark-modal .modal-content {
    background-color: red;
    color: white;
  }

.dark-modal .close {
    color: white;
}

.light-blue-backdrop {
    background-color: blue !important;
}

您看到的图像背景显示为黑色而不是蓝色:

【问题讨论】:

    标签: angular bootstrap-ui


    【解决方案1】:

    你需要使用 ViewEncapsulation.None(*)

    在你的组件中

    import { Component,ViewEncapsulation  } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      encapsulation: ViewEncapsulation.None, //<--this line
      ...
    })
    

    stackblitz

    (*)小心,ViewEncapsulation.None,让组件中的所有 .css 传播到所有应用程序,所以如果你在 .css 中有类似的东西

    h1{color:red}
    

    您在应用程序中的所有 h1 都变为红色。为避免这种情况,您可以将所有组件包含在一个带有类的 div 中,并像这样编写 .css

    .mycomponent.h1{color:red}
    

    【讨论】:

    • 使用 ViewEncapsulation.None 可以正常工作,但是 .... 破坏了我正在工作的其他 Web 样式,例如带角度的动画效果。
    【解决方案2】:

    只需添加具有背景定义的全局样式(样式文件夹)。 ng-bootstrap modals的背景被注入到body标签中。

    不要更改应用 ViewEncapsulation,尤其是对于应用组件...这会给你带来很多问题。

    【讨论】:

    • 你写的网址不好。正如您所说,ViewEncapsulation 不是解决方案,因为破坏了我在网络中拥有的其他 flex 样式。请问你能写下有效的网址吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    相关资源
    最近更新 更多