Michael Michael - 29 days ago 22
AngularJS Question

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?

Answer
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.