1

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;
}
2
  • try this , d.setSeconds(d.getSeconds() + 30);
    – rejo
    Jul 28, 2016 at 8:50
  • Oh my.. Thanks a lot, such a silly mistake..
    – MatMat
    Jul 28, 2016 at 8:57

1 Answer 1

1

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);
       

2
  • I will, but I can't do it yet :-)
    – MatMat
    Jul 28, 2016 at 8:58
  • 1
    Yes, but you can't accept an answer before 3-5 minutes has passed
    – MatMat
    Jul 28, 2016 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.