简体   繁体   中英

ngBootstrap Modal is not showing unless after second click

I have created a modal component called ImportCardModalComponent . This component must be opened if the login is failed. like follows:

this.authSerivce.logInRegular(this.model).then(result => {
      console.log(result);
    }, error => {
      var importModal = this.modalService.open(ImportCardModalComponent);

    });

The issue is that the dialog doesn't appear unless I click the button on screen twice and fire the service two times. The first time I click the button, DOM elements are added successfully but without css class in <ngb-modal-backdrop> and <ngb-modal-window> . As shown below. 未定义类 The second time I click on the button, the classes are showing correctly. As show below: 在此处输入图片说明

The modal MUST have class ="modal-backdrop fade show" in backdrop element. As well as class="modal fade show d-block" in window element.

I tried to use the modalService with NgbModalOptions backdropClass and windowClass without any success to work from first time.

If I move the open modal service outside the reject callback, it works fine.

Any Help is much appreciated.

One way is to manually trigger change detection after modal service call.

Get the reference of ApplicationRef

constructor(private appRef: ApplicationRef,...){} 

and then invoke change detection:

this.authSerivce.logInRegular(this.model).then(result => {
      console.log(result);
    }, error => {
      var importModal = this.modalService.open(ImportCardModalComponent);
      this.appRef.tick();
    });

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