admin管理员组文章数量:1327969
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');
}
本文标签: javascriptSet a cookie in Angular if one does not existStack Overflow
版权声明:本文标题:javascript - Set a cookie in Angular if one does not exist - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742173059a2427072.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论