Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
Best way to close ngbModal instances when back button is pressed

In Angular app(ver 11) when back button is pressed or location.back() is called if ngbModal is active I want it to be closed. After research that I made I came up with something like this:

  modalService.activeInstances.subscribe((result: any) => {
      if (result.length > 0) {
        history.pushState(null, null, window.location.href);
      }
    })
  @HostListener('window:popstate', ['$event'])
  onBrowserBackBtnClose(event: Event) {
    event.preventDefault();
    event.stopPropagation();
    this.modalService.dismissAll();
  }

This didn't work like many other solutions I tried as mentioned on the internet. I want to decide if anyModal instances active or not. If it is, then close when back button is pressed; otherwise just go back.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

just add AngularLocator service and call it on ngOnInit() of any component you need to close the modal!

Angularlocater.onUrlChange(() => this.modalService.dismissAll());
about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is the best I did

 lastModalInstance: NgbActiveModal;


  modalService.activeInstances.subscribe((activeModals: NgbActiveModal[]) => {
      if (activeModals.length > 0) {
        if (activeModals.length === 1) {
          history.pushState(null, null, window.location.href);
        }
        this.lastModalInstance = activeModals[activeModals.length - 1];
      }
      else {
        this.lastModalInstance = null;
      }
    })

  @HostListener('window:popstate', ['$event'])
  onBrowserBackBtnClose(event: Event) {
    event.preventDefault();
    if (this.lastModalInstance) {
      this.lastModalInstance.close();
    }
  }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda