How to remove all cookies in Angularjs?

后端 未结 2 1471
情歌与酒
情歌与酒 2020-12-14 17:45

I can set a cookie like this:

$cookieStore.put(\'myCookie\',\'I am a cookie\');

And I can remove it with

$cookieStore.remov         


        
相关标签:
2条回答
  • 2020-12-14 18:31

    Ok, obviously this may not be the best solution, but I've find a workaround:

    angular.forEach($cookies, function (v, k) {
        $cookieStore.remove(k);
    });
    

    But I'ld still appreciate if there's a better solution. I'm really curious about why there isn't a built-in $cookieStore.removeAll() method...

    Requires the ngCookies module to be installed.

    Edit

    With the 1.4 version, $cookieStore is deprecated. Instead you can use $cookies service. Get all cookies with $cookies.getAll() and remove each with $cookies.remove('key').

    var cookies = $cookies.getAll();
    angular.forEach(cookies, function (v, k) {
        $cookies.remove(k);
    });
    
    0 讨论(0)
  • 2020-12-14 18:37

    In case you are on this page and you working on an old project that uses angular 1.3.x or less, you can simply use this

    $cookies.cookieKey = undefined;
    delete $cookies['cookieKey'];
    
    0 讨论(0)
提交回复
热议问题