简体   繁体   中英

Angular 8 ng-bootstrap modal not loading CSS classes

I npm install @ng-bootstrap/ng-bootstrap@5.3.1 in an Angular 8 project, When I open the modal using the modal service, the first try the modal will take time to open. the second time the modal does not open. Looking at the devtool, the modal is open just doesn't have CSS classes set. I did import bootstrap to the styles.scss file @import "~bootstrap/scss/bootstrap.scss"; I tried using JavaScript to inject the CSS classes after open the model but didn't make any difference.

   @ViewChild('modalContent', { static: false }) private modalContent: TemplateRef<any>;
   // .....some code here....

    public openNgbModal(): NgbModalRef {
      return this.modalService.open(this.modalContent, {
        size: 'lg',
        backdrop: 'static',
        keyboard: false,
        windowClass : 'modal-content'
      });
    }

See below:

在此处输入图像描述

The correct html should be the below: 在此处输入图像描述

Try calling the modalService.open() without any options and see if it make any difference.

this.modalService.open(this.modalContent)

Im not proud of the answer but it did get the job done. Used javascript to add the classes manual.

   public openNgbModal(): NgbModalRef {
      return this.modalService.open(this.modalContent, {
        size: 'lg',
        backdrop: 'static',
        keyboard: false,
        windowClass : 'modal-content'
      });

      const modalBackdrop = document.getElementsByTagName('ngb-modal-backdrop');
      // check the class if exists
      modalBackdrop[0].className = 'modal-backdrop fade show';
      
      const modalWindow = document.getElementsByTagName('ngb-modal-window');
      // check the class if exists
      modalWindow[0].className = 'modal fade show d-block';
      modalWindow[0].setAttribute('aria-modal', 'true');
      modalWindow[0].children[0].className = 'modal-dialog modal-s';
    }

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