0

I am working in an Angular 4 application ,In my application I have 5 components What I am trying to do is ,When the user close the browser window from any one of the five components I need to call the API and say the user is closed the window on this time .

In my case the API is not get called when I closed the application.

app.component.ts

export class AppComponent implements OnInit {

constructor(private CartdataService: CartdataService, private http: HttpClient) { }

    ngOnInit() {}

    @HostListener('window:beforeunload', ['$event'])
        beforeunloadHandler(event) {
            this.WindowClosed();
        }

        WindowClosed() {

            this.CartdataService.get_DummyCall().subscribe();
        }
}

Here I haven't implemented anything other than the above code . Can anyone tell me Where I did the mistakes.

Reference :

Angular 2 - Execute code when closing window

Nikson
  • 900
  • 2
  • 21
  • 51

1 Answers1

0

Finally got it ...

@HostListener('window:beforeunload', ['$event'])
    public beforeunloadHandler($event) {
    this.CartdataService.get_DummyCall().subscribe();
   }
Nikson
  • 900
  • 2
  • 21
  • 51
  • is there any way to show confirm box before call api? – SKL May 21 '18 at 08:27
  • This doesnot work for me. I do a call to do splunk log but I cannot see log in database. @HostListener('window:beforeunload', ['$event']) private beforeunloadHandler($event) { this.logHelper.logApplicationClose(this.footerUser, this.footerUserRole, this.clientIPAddress); } – Ziggler Jun 15 '18 at 21:13