$scope.unload = function () { myService.sendSomethingToDatabase({ userId: $scope.user.id}); } $scope.$on(\'$destroy\', function () { $scope.unload();