Ng-bootstrap: Cannot close the Modal through the NgbModalRef

Created on 11 Sep 2016  路  3Comments  路  Source: ng-bootstrap/ng-bootstrap

I have a Modal with a form in it, and I want to close it on successful submit.
I save the NgbModalRef as a class property on the open method and im executing this code on submit:

close modal on submit
(had to create the localModalRef because of scope issues)

As soon as it gets to the close() execution the console shouts:

localModalRef.close is not a function

Cant seem to figure out what have i done wrong.

Thanks in advance.

Most helpful comment

This should be posted on StackOverflow rather than here.

And you should always post your code as text, rather than as an image, since it makes it easier to read, copy and paste.

Anyway, you're not initializing this.modalRef with the NgbModalRef (BTW, TypeScript should warn you about that, if you specify the type of the modalRef property). You're initializing it with the promise returned by then() on the result property of NgbModalRef.

The code should be:

private modalRef: NgbModalRef;

// ...

open(content) {
  this.modalRef = this.modalService.open(content);
  this.modalRef.result.then(...);
}

All 3 comments

This should be posted on StackOverflow rather than here.

And you should always post your code as text, rather than as an image, since it makes it easier to read, copy and paste.

Anyway, you're not initializing this.modalRef with the NgbModalRef (BTW, TypeScript should warn you about that, if you specify the type of the modalRef property). You're initializing it with the promise returned by then() on the result property of NgbModalRef.

The code should be:

private modalRef: NgbModalRef;

// ...

open(content) {
  this.modalRef = this.modalService.open(content);
  this.modalRef.result.then(...);
}

You're absolutely right, sorry for posting here, thanks a lot

This should be posted on StackOverflow rather than here.

And you should always post your code as text, rather than as an image, since it makes it easier to read, copy and paste.

Anyway, you're not initializing this.modalRef with the NgbModalRef (BTW, TypeScript should warn you about that, if you specify the type of the modalRef property). You're initializing it with the promise returned by then() on the result property of NgbModalRef.

The code should be:

private modalRef: NgbModalRef;

// ...

open(content) {
  this.modalRef = this.modalService.open(content);
  this.modalRef.result.then(...);
}

you saved my day. Thank you so much dude.

Was this page helpful?
0 / 5 - 0 ratings