MatMat MatMat - 4 months ago 18
AngularJS Question

Cookie is still present after expiration date

So I'm trying to use the ngCookie ($cookies) to set a boolean in AngularJs.
As you can see in the example below, the "someFlag" should only be true, if the cookie is still present. The cookie expiration is set to 30 seconds, but even after 30 seconds, the "someFlag" is still true.

So how do I manage to find out wether the cookie is expired or not?

var peekerHideCookie = $cookies.get('peekerHideCookie');
console.log(peekerHideCookie);
if (peekerHideCookie == 'true'){
$scope.someFlag = true;
}

$scope.test = function () {
var d = new Date();
d.setDate(d.getSeconds() + 30);
$cookies.put('peekerHideCookie', 'true', {expires: d});
$scope.someFlag = true;
}

Answer

Use d.setSeconds(d.getSeconds() + 30) insetead of d.setDate()

 $scope.test = function () {
        var d = new Date();
        d.setSeconds(d.getSeconds() + 30);
        $cookies.put('peekerHideCookie', 'true', {expires: d});
        $scope.someFlag = true;
    }

var d = new Date();
console.log(d);
d.setSeconds(d.getSeconds() + 30);
console.log(d);
       

Comments