Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NgbModal error when trying to close

I've successfully imtegrated the NgbModal into my Angular 2 application, I currently have another component being shown within the modal.

The issue I have is once it's appeared and I click close I get the following error message:

Cannot read property 'close' of undefined

Now I've been following Components as content I've looked at the HTML and also gone through the Typescript, however I'm unsure what I'm actually missing here.

This is my type script file:

import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { BugService } from '../service/bug.service';
import { Bug } from '../model/bug';
import { NgbModal, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { BugDetailComponent } from '../../bug-detail/bug-detail.component';

@Component({
   selector: 'bug-list',
   templateUrl: './bug-list.component.html',
   styleUrls: ['./bug-list.component.css']
})

export class BugListComponent implements OnInit {

  private bugs: Bug[] = [];

  constructor(private bugService: BugService, private cdRef: ChangeDetectorRef, private modalService: NgbModal) { }

  ngOnInit() {
    this.getAddedBugs();
  }

  getAddedBugs() {
    this.bugService.getAddedBugs().subscribe(bug => {
        this.bugs.push(bug);
        this.cdRef.detectChanges();
    },
        err => {
            console.error("unable to get added bug - ", err);
        });
  }

  open() {
     const modalRef = this.modalService.open(BugDetailComponent);
     modalRef.componentInstance.name = 'World';
  }
}

I import BugDetailComponent which I then reference inside the open() function. When I click close after the modal has appeared that's when I see the error message.

My HTML is as follows:

<div class="modal-header" [id]="modalId">
<button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
    <span aria-hidden="true">&times;</span>
  </button>
<h4 class="modal-title">Hi there!</h4>
</div>
<div class="modal-body">
  <p>Hello, {{name}}!</p>
</div>
<div class="modal-footer">
   <button type="button" class="btn btn-secondary" (click)="activeModal.close('Close click')">Close</button>
</div>

Could someone please shed some light into why I receive this error and maybe help me fix it?

like image 341
Code Ratchet Avatar asked Oct 18 '22 19:10

Code Ratchet


1 Answers

Please ignore, I fixed this. Was missing the following constructor off the bug-detail.component.ts

constructor(public activeModal: NgbActiveModal) {}
like image 137
Code Ratchet Avatar answered Oct 21 '22 06:10

Code Ratchet