am trying to implement service call when closing the browser tab or window. Is there any way to call RestApi trying to close the browser tab or window.
Are there any suggestions?
am trying to implement service call when closing the browser tab or window. Is there any way to call RestApi trying to close the browser tab or window.
Are there any suggestions?
This is a bad idea for a few reasons. The closing of the browser does not mean they have left because they can just re-open or even refresh the page (yes refreshing the page is same as closing the browser). However to directly answer your question yes you can by using the injected $http
provider in your controller (assuming you did not mean the angular 2+ but actually the old angular 1.x):
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
window.addEventListener('beforeunload', function () {
$http({ method : "GET", url : "your endpoint url here" })
.then(function (response) {
$scope.myVariable = response.data;
},
function (response) {
$scope.myVariable = response.statusText;
});
}
});
Please use the following code to run the function on close the browser
constructor() {
window.onbeforeunload = ()=>{
//call API here
}
}