how to call any service while closing window/tab I have tried the beforunload and canDeactive When I close the browser service get hitting in chrome debugger but not reaching the actual service any hacks there pls help
Asked
Active
Viewed 2,678 times
-2
-
1Hi Deepak, you need to give more detail about what you are trying to achieve. I suggest that you put together the code you have using www.stackblitz.com happy to look at this if you create one. You might also might find something looking at this question: https://stackoverflow.com/questions/38999842/angular-2-execute-code-when-closing-window which yours seems to be a duplicate of.. – nclarx Feb 07 '19 at 12:25
2 Answers
2
Finally I able to make it work with the unloadNotification event its work in IE Also. should useful for anyone..
@HostListener('window:beforeunload', ['$event'])
unloadNotification($event: any) {
console.log(JSON.stringify($event) + '#event')
if (!this.canDeactivate()) {
this.cancelTransaction();
$event.returnValue = "Are you sure you want close?)";
}

Deepak
- 571
- 7
- 19
-1
Yes you can call any service by closing a page.You need to call service in hooks lifecycle ngOnDestroy() method.
ngOnDestroy(){
//call your service here.
}
Reference:-

Karnan Muthukumar
- 1,843
- 1
- 8
- 15
-
This will not work because ‘onDestroy’ is only fired when the component is removed from the DOM, not when the tab is closed. – nclarx Feb 07 '19 at 15:43
-
-
-