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

209
Visualizações
How to update modified browser history on forward button when active modal is closed?

In angular app I had to modify history stack to close active ngbModal on browser's back button.

In app.component.ts I injected modalService like private readonly modalService: NgbModal to make this component aware of activeModals.App component must be the place where I should apply changes because I have lots of modal around 100 in the app so I can't write code in all these components where modals used.

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

In this implementation I'm listening activeModals array and I want to close modals one by one on each browser's back button click.Once a modal is active I'd like to modify state not to goBack once its clicked.Whenever this observable recieves new activeModals array last one should be picked and assigned to lastModalInstance variable.When all modals are closed empty array is emitted so variable lastModalInstance should be null.

  @HostListener('window:popstate', ['$event'])
  onBrowserBtnClicked(event: Event) {
    event.preventDefault();
    if (this.lastModalInstance) {
      this.lastModalInstance.close();
    }
  }

In the end event popstate is being triggered on each forward-back button clicked in browser.When back button clicked,if active modal exists it should be closed otherwise app should be navigated back.

This implementation working quite good for clicking back.But clicking forward breaks my logic.I couldn't figure out what to do also to handle forward button.Is there anyone who can help me with this? I'm also open for other solutions. (Angular 11 is used for this)

about 4 years ago · Juan Pablo Isaza
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