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?
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.
We already have an NPM package for Angular called 'ngx-cookie-service' that can be used for cookie use.
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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With