There are several reasons why this may not be the best approach. Simply closing the browser does not necessarily mean that the user has left as they could easily reopen or refresh the page (which essentially achieves the same result as closing the browser). However, to address your specific question, it is indeed possible by utilizing the injected $http
provider in your controller (assuming you are referring to Angular 1.x rather than Angular 2+):
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;
});
}
});