4
votes

i'm using the ng-bootstrap modal popup, and it's not closing on click of the cross button.

This is the <a> tag that triggers the popup -

<div class="actions padding-zero">
    <a href="javascript:void(0);" (click)="openFilter()" class="icon configure-columns-icon">
        <span class="control-label">Configure Columns</span>
    </a>
</div>

Modal -

<ng-template #filterForm let-modal>
    <div class="TitlePanel">
    Configure Columns                       
    <button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')">
    X
    </button>
    </div>
    <div class="modal-body">
      Body
    </div>                   
</ng-template>

component.ts file -

export class NgbdModalContent {
    @Input() name;
    constructor(public activeModal: NgbActiveModal) { }
}


@Component({
    selector: 'app-modals',
    templateUrl: './modals.component.html',
    styleUrls: ['./modals.component.scss'],
    encapsulation: ViewEncapsulation.None,
})

export class ModalsComponent {

    closeResult: string;

    constructor(private modalService: NgbModal) { }

    // Open default modal
    open(content) {
        this.modalService.open(content).result.then((result) => {
            this.closeResult = `Closed with: ${result}`;
        }, (reason) => {
            this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
        });
    }

    // This function is used in open
    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}`;
        }
    }

    // Open modal with dark section
    openModal(customContent) {
        this.modalService.open(customContent, { windowClass: 'dark-modal' });
    }

    // Open content with dark section
    openContent() {
        const modalRef = this.modalService.open(NgbdModalContent);
        modalRef.componentInstance.name = 'World';
    }
}

Also, I'm getting this error in the console when i click the close button - "Cannot read property 'dismiss' of undefined"

enter image description here

4
Your template references an openFilter method, but this isn't shown in your code.Alex Peters
Can you create Stack blitz demo... so we can help youAniket Avhad
What is the version of ng-bootstrap that you are using? The let-modal syntax only works from 3.2.0.pkozlowski.opensource
@pkozlowski.opensource - v3.2.0.Sagar Raj
can you please put your code using stackblitz.comcore114

4 Answers

13
votes

After some research and making these changes, it worked.

<ng-template #filterForm let-d="dismiss">
    <div class="TitlePanel">
        Configure Columns                       
        <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
        X
        </button>
    </div>
    <div class="modal-body">
       Body
    </div>                   
</ng-template>
3
votes
(click)="modal.dismiss('Cross click')"

needs to be changed to

(click)="activeModal.dismiss('Cross click')"

because you are giving

public activeModal: NgbActiveModal
1
votes

You are using modal but you are injecting activeModal. Update the html code to this:

<ng-template #filterForm let-modal>
    <div class="TitlePanel">
    Configure Columns                       
    <button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
    X
    </button>
    </div>
    <div class="modal-body">
      Body
    </div>                   
</ng-template>
0
votes
[solution by   using modal reference][1]

[1]: Angular 2 ng-bootstrap close Modal It works