I want to see if a cookie has been set in Angular, and if it doesn't find a cookie, it will create a new one.
Right now I'm using this:
if ($cookies.get('storedLCookie').length != 0) {
$cookies.put('storedLCookie','Oatmeal');
}
It doesn't seem to be working, though. Is there another way to test if a cookie is set in Angular?
I want to see if a cookie has been set in Angular, and if it doesn't find a cookie, it will create a new one.
Right now I'm using this:
if ($cookies.get('storedLCookie').length != 0) {
$cookies.put('storedLCookie','Oatmeal');
}
It doesn't seem to be working, though. Is there another way to test if a cookie is set in Angular?
Share Improve this question asked Mar 23, 2016 at 6:22 ArtvaderArtvader 9585 gold badges16 silver badges32 bronze badges2 Answers
Reset to default 6you can simply do this to check it:
var favoriteCookie = $cookies.get('cookieval');
if ( favoriteCookie ) {
//exists
} else {
//not exists
}
The data store in cookie is string or number. When it's a number, you should't judge it by length. Test the following:
var a = 3
console.log(a.length) // will return undefined
I think you can just write the following instead:
if ($cookies.get('storedLCookie')) {
$cookies.put('storedLCookie','Oatmeal');
}