简体   繁体   中英

angular/ngbmodal: backdropClass doesnt work

I am trying to change background color of ngbmodal dialog to blue using backdropClass . It doesn't work, why?

my code is:

* component call:

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

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

*html component:

<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 classes:

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

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

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

The image you see appears background in black instead of blue:

在此处输入图像描述

Simply add a global style (styles folder) with a background definition of a backdrop. Backdrops of ng-bootstrap modals are injected into body tag.

Don't change app ViewEncapsulation, particularly for app component... It's gonna cause you a lot of problems.

You need use ViewEncapsulation.None(*)

In your component

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

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

See stackblitz

(*)Be carefull, ViewEncapsulation.None, make that all the.css in your component propagate to all the application, so if you has in.css some like

h1{color:red}

All yours h1 in the application becomes color red. To avoid it you can enclose all your component in a div with a class and write your.css like

.mycomponent.h1{color:red}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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