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

129
Views
Angular Warning Message Before Exiting a Page

In my app, I want a feature that gives a warning message when I try to; close the tab, exit the page or reload the page. The code I wrote for it is below. Right now, it gives a warning. Yet, it still exits the page before I can click “yes” or “no”. How can I fix this?

HTML:

<div (window:beforeunload)="onWindowClose($event)"></div>

TS:

    @HostListener('window:beforeunload', ['$event'])
onWindowClose(event: any): void {

 this.confirmDialogRef = this._dialog.open(FuseConfirmDialogComponent, {
    disableClose: false,
});
this.confirmDialogRef.componentInstance.confirmMessage =
    "You will be exiting the page, are you sure?";

this.confirmDialogRef.afterClosed().subscribe((result) => {});

  event.preventDefault();
  event.returnValue = false;



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

0

Why not use a Directive to achieve this

@Directive({selector: 'button[counting]'})
class CountClicks {
  numberOfClicks = 0;

  @HostListener('click', ['$event.target'])
  onClick(btn) {
    console.log('button', btn, 'number of clicks:', this.numberOfClicks++);
 }
}

@Component({
  selector: 'app',
  template: '<button counting>Increment</button>',
})
class App {}

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!