NgbModal.open not working in Angular 4
Asked Answered
A

2

5

I want to display a message in a modal using angular 4.0.0 and ng-bootstrap 1.0.0-beta.4 but it does not display the modal.

app-module.ts

@NgModule({
// ...
declarations: [
  LoginComponent
],
imports: [
// ...
  NgbModule.forRoot()
],
entryComponents: [LoginComponent],
})
export class AppModule { }

login.component.ts

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent {
   @ViewChild('examplemodal')
   private modalRef: TemplateRef<any>;

   constructor(private modalService: NgbModal) { }

   //.... some event that fires onModalRequest() 

   onModalRequest(): void {
    const modalRef = this.modalService.open(this.modalRef); //Not working

    modalRef.result.then((result) => {
      console.log(result);
      console.log('closed');
    }).catch( (result) => {
      console.log(result);
      console.log('cancelling');
    });
  }
}

examplemodal.html

<ng-template #examplemodal>
  <div class="modal">
    stuff here...
  </div>
</ng-template>

Any help please?

Almsgiver answered 12/3, 2018 at 21:20 Comment(11)
According to the API: Content can be provided as a TemplateRef or a component type. And you provide string.. Source: ng-bootstrap.github.io/#/components/modal/apiFlashback
You're right! Please check my edit, I already added a TemplateRef but still not working. @echonaxAlmsgiver
Any errors in this one?Flashback
@RafaelReyes - Did you include Bootstrap 4 CSS in your project? I think that the version of ng-bootstrap that you use requires an older version of Bootstrap (something like 4.0.0-beta.3).Houseleek
No I'm using the Bootstrap 3.3.7 @ConnorsFanAlmsgiver
I doubt that ng-bootstrap can work with Bootstrap 3.Houseleek
When I use Bootstrap 4 I get an error from bootsatrp css : Module build failed: BrowserslistError: Unknown browser major @ConnorsFanAlmsgiver
Check the solution given by oopsmails in this issue. He recommends dropping to 4.0.0-beta.2.Houseleek
It works with angular 5 .. I'm using angular 4 @ConnorsFanAlmsgiver
any errors in console ? @RafaelReyesLegit
No, it just does not display the modal. @RahulAlmsgiver
L
8

Here is how I implemented in my application:

app.module.ts

@NgModule({
// ...
declarations: [
  LoginComponent,
  ModalComponent
],
imports: [
// ...
  NgbModule.forRoot()
],
entryComponents: [ModalComponent], //Only Modal component included here
bootstrap: [AppComponent]
})
export class AppModule { }

Component from where I am opening the modal. LoginComponent in your case :

imports ....
import { ModalComponent} from '../Modal/Modal.component'; //My actual Modal component which includes HTML for modal

    @Component({
      selector: 'app-login',
      templateUrl: './login.component.html',
      styleUrls: ['./login.component.css']
    })
    export class LoginComponent {
       @ViewChild('examplemodal')
       private modalRef: TemplateRef<any>;

       constructor(private modalService: NgbModal) { }

       //.... some event that fires onModalRequest() 

       onModalRequest(): void {
        const modalRef = this.modalService.open(ModalComponent); //Added Modal Component here

         modalRef.componentInstance.src = link;//anything u wish to pass to modal component @Input 

 modalRef.result.then((result) => {
      console.log(result);
      console.log('closed');
    }).catch( (result) => {
      console.log(result);
      console.log('cancelling');
    });

      }
    }

Modal.component.ts:

import { Component, OnInit, Input } from '@angular/core';
import { NgbModal, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
@Component({
  selector: 'app-modal',
  templateUrl: './modal.component.html',
  styleUrls: ['./modal.component.scss']
})
export class ModalComponent implements OnInit {
  @Input() src;
  constructor(public activeModal: NgbActiveModal) { }

  ngOnInit() {
  }

}

Modal.component.html:

<div class="modal-header">
  <button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
    <span aria-hidden="true">&times;</span>
  </button>
</div>
<div class="modal-body">
          STUFF HERE
</div>

Here working Demo

PLEASE NOTE: You would need to install Bootstrap 4 for SCSS. npm install bootstrap -D and include its reference in your style.scss like :

@import "node_modules/bootstrap/scss/bootstrap";
Legit answered 14/3, 2018 at 5:30 Comment(0)
R
0

In my case the modal didn't show while it was invoked inside ngOnInit(). The solution is to wrap it using setTimeout(). I know the solution is not pretty, but maybe someone will save their time

setTimeout(function(){
   this.modalService.open(this.content, { centered: true });
}.bind(this), 1000);
Reich answered 27/3, 2023 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.