Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a cookie in Angular if one does not exist

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?

like image 874
Artvader Avatar asked Dec 08 '25 08:12

Artvader


1 Answers

you can simply do this to check it:

var favoriteCookie = $cookies.get('cookieval');

  if ( favoriteCookie ) {
    //exists
  } else {
    //not exists
  }
like image 50
Yassine Qoraiche Avatar answered Dec 09 '25 21:12

Yassine Qoraiche



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!