Close / leave an event page using AngularJS

I am trying to implement a one-page application that starts with a login form. For the front end I use AngularJS.

In the login controller, I check the authentication data, and if everything is ok, I set a cookie (using the $ cookieStore service) with some user data and send it to a different view. Here, in the controller of the second view, I check if the user is empty from the cookie, and if so, I redirect to the login view.

What I want to do is when the user closes the browser or, if he leaves the page, removes the cookie. I tried to use:

$scope.$on("$locationChangeStart", function(){
    $cookieStore.remove('user');
});

or

$scope.$on("$destroy", function(){
    $cookieStore.remove('user');
});

. , (cookie ) , / (cookie ). URL- , , cookie , .

- ?

+4
2

. , , , - .

:

$scope.$on("$locationChangeStart", function(event){
    event.preventDefault()
    $cookieStore.remove('user');
});
0

,

 $scope.$on('$locationChangeStart',function(event) {
    if($scope.formsubmitted && $scope.myForm.$dirty){   
        var answer = confirm("Are you sure you want to leave this page?")
            if (!answer) {
            event.preventDefault();
        }else{
          $cookieStore.remove('user');
        }
    }
 });
0

All Articles