Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all cookies in Angularjs?

Tags:

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?

like image 398
s.alem Avatar asked Oct 28 '14 12:10

s.alem


People also ask

What is used to read or write a cookie by AngularJS?

AngularJS uses ngCookies module and $cookieStoreservice to carry out the various functions of reading, writing and removing Cookies. The below HTML Markup consists of an HTML DIV to which ng-app and ng-controller AngularJS directives have been assigned.

Can we use cookies in angular?

We already have an NPM package for Angular called 'ngx-cookie-service' that can be used for cookie use.


1 Answers

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); }); 
like image 154
s.alem Avatar answered Sep 28 '22 06:09

s.alem