0

I'm adding a basic modal in Angular 4. The issue it's that it looks like the modal it's appearing in the backdrop of the page.

I have a profile.component.ts. Basically i have:

import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; 
constructor(private modalService: NgbModal){}

open(){
      this.modalService.open('Hello there');
  }

In profile.component.html i have a button calling the open() function. When clicking at the button, the only thing that occurs it's the scroll bar appearing and disappearing.

I'm guessing that something is missing.

Thanks in advance.

5
  • Have you added some content? ng-bootstrap.github.io/#/components/modal/examples Commented Dec 26, 2017 at 21:53
  • Yes, i tried with that. And the same. So i tried to take an easy way, at least for testing if something pops. I'm following this simple plunker example, plnkr.co/edit/5qq04Q4HFLOe9tsyeMMI?p=preview @JonathanDion Commented Dec 26, 2017 at 21:58
  • Maybe it's a CSS issue. Did you add the CSS file too? Commented Dec 26, 2017 at 22:02
  • yes i have the bootstrap cdn files added Commented Dec 26, 2017 at 22:16
  • Can you put all your code in a sandbox? Like plunker. I will take a look. Commented Dec 26, 2017 at 22:25

1 Answer 1

1

When you are calling open() and just sending the string will not work. you should send Id instead.

You can follow this example and it will work perfectly fine.

Code.ts

import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap'; 
constructor(private modalService: NgbModal){}

open(){
      this.modalService.open(sayHello);
  }

private getDismissReason(reason: any): string {
if (reason === ModalDismissReasons.ESC) {
    return 'by pressing ESC';
} else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
    return 'by clicking on a backdrop';
} else {
    return `with: ${reason}`;
}
}

Code.html

<ng-template ngbModalContainer></ng-template>
<ng-template #sayHello let-c="close" let-d="dismiss">
    <div class="modal-header ">
        <h6 class="modal-title text-uppercase ">Headding</h6>
        <button type="button " class="close " aria-label="Close " (click)="d( 'Cross click') ">
      <span aria-hidden="true ">&times;</span>
    </button>
    </div>
    <div class="modal-header ">
       HELLO :)
    </div>
    <div class="modal-footer">
        <button class="btn " (click)="d( 'Cross click') ">Close</button>
    </div>
</ng-template>

and don't forget to import

import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; in your app.module.ts

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';    
imports: [ NgbModule.forRoot(),
    ...
    ...
    ]

I think this should help.

Sign up to request clarification or add additional context in comments.

3 Comments

can you be more specific about your question @sree
I am new to Angular 4 and i don't know in which file we need to keep the code of code.html file from above
Whichever component you need your model to appear you can place the code there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.