Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
Is there a way to run a specific function before moving or refreshing pages in angular?

When a page is routed to another page or when a user presses the Back, Forward, or Refresh buttons, the alert window is displayed and the user selects Move, and the user wants to proceed with the initialization process.

For unload events, this does not occur in page routing. Attempted to use canDeactivate, but failed to execute a specific function after closing the alert window.

I want to know the detailed method and example. (I'm not familiar with the anglular)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Yes. I can think of two potential solutions to your problem:

-1- If you are using Angular Material Dialog combined with canDeactivate guard logic there is a built-in method just called like that. It is named beforeClosed. It returns an Observable that you can subscribe to which will notify you when the dialog started closing. Inside this method you can execute your function.

-2- Angular router has an API called events. It essentially helps you track the status of the router. It can detect when the router starts the navigation, ends the navigation, cancels the navigation, resolved the navigation, etc... For a complete list of API advanced events. Check this documentation link Cool, with this API you can choose the right event that suits you and put you function inside the condition such as :

checkRouterEventToFireCustomFunctionLogic(){
    this.router.events.subscribe((routerEvent) => { 
    if(routerEvent instanceof NavigationStart) { 
     // execute my custom function OR 
     // just console.log("Hello World")
    } 
  });
}

PS: beforeunload event is not really good especially when it comes to Web Vitals bfcache

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!