Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda