gnc
gnc

Reputation: 41

How to close ngx-bootstrap modal on router.navigate

I'm using ngx-bootstrap ModalService. I have an interceptor that redirects the user to login page based on some condition using router.navigate(['/login']) . Redirection works fine but the modal stays open. I know I could implement OnDestroy and close it there, but I'm not sure if it's a good idea to do that on every component that displays a modal, seems repetitive. Is there a way to close a modal / all (possible) open modals at redirect time (doing it on the interceptor also feels I'm coupling stuff). I apologize If I'm not asking the question the right way, this is my first time. If you need further details, please let me know. Thanks in advance.

Upvotes: 1

Views: 1249

Answers (1)

Vinaayakh
Vinaayakh

Reputation: 522

you could listen for router changes and close the model when a route changes

router.events.subscribe((val) => {
    // close the router here
});

you can add this in the main component or add it in a service

Upvotes: 2

Related Questions