如何清除Angularjs中的所有Cookie? [英] How to remove all cookies in Angularjs?

查看:1647
本文介绍了如何清除Angularjs中的所有Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以设置一个这样的cookie:

  $ cookieStore.put('myCookie' ); 

我可以使用

删除它

  $ cookieStore.remove('myCookie'); 

但是如何删除所有Cookie?

解决方案

好吧,显然这可能不是最好的解决方案,但我找到一个解决方法:

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

但我还是会欣赏,如果有一个更好的解决方案。我真的很好奇为什么没有内置的 $ cookieStore.removeAll()方法...



需要安装 ngCookie 模块。



编辑 b $ b

对于1.4版本, $ cookieStore 已被弃用。您可以使用 $ cookies 服务。使用 $ cookies.getAll()获取所有Cookie,并使用 $ cookies.remove('key')删除每个。

  var cookies = $ cookies.getAll(); 
angular.forEach(cookies,function(v,k){
$ cookie.remove(k);
});


I can set a cookie like this:

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

And I can remove it with

$cookieStore.remove('myCookie');

But how can I remove all cookies?

解决方案

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);
});

这篇关于如何清除Angularjs中的所有Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆