Michael
Michael

Reputation: 13616

Why cookies not saved after I close browser?

I use angularjs1.5.5 in my project.

I use $cookies service in my project.I create and store some credentials in the cookie:

this.setCookie = function (inspectorName) {
    $cookies.put('inspectorName', inspectorName, {
        expires: new Date(new Date() + 1, new Date().getMonth(), new Date().getDate())
    });
}

And here how I access the cookies:

var credentials = $cookies.get('inspectorName');

But when I close and open the page and I try to get cookies by row above I get 'undefined'.

Any idea why cookies not saved after closing browser?

Upvotes: 2

Views: 4407

Answers (1)

Nikhil Mohanan
Nikhil Mohanan

Reputation: 1260

new Date(new Date() + 1, new Date().getMonth(), new Date().getDate())

expires value is going as null.So its taking the expiry time as browser session.Means life of the cookie will expire on browser close .add a valid future date and i am sure it will work.

Upvotes: 6

Related Questions