How to clear cookies in angular.js

假装没事ソ 提交于 2019-11-30 21:49:59

问题


Currently am using angulajs app.

I want to store some values in cookie. So i used angular-cookies-min script for add some values to cookies

I have used this below code for save values to cookie.

 $cookieStore.put("userInfo", userInfo);//userInfo is a array with some values. 

Now I want to clear the cookie? How can i do it?


回答1:


Try this code for delete cookie

$cookieStore.remove("userInfo");

EDIT: Since v1.4 $cookieStore has been deprecated (see docs), so from that version on you should use:

$cookies.remove("userInfo");



回答2:


I have found some answer

Option 1

delete $cookies["userInfo"]

option 2

 .controller('LogoutCtrl', ['$scope',  '$cookies', function ($scope,$cookies) {

    $cookies.userInfo = undefined;

}]);



回答3:


angular.forEach($cookies, function (cookie, key) {
    if (key.indexOf('NAV-') > -1) {
         $window.sessionStorage.setItem(key, cookie);
         delete $cookies[key];
    }
 });

The above code works for me however 'Resources' tab of Chrome inspection utility continues to show that the cookies are not deleted. I verified that the cookies are indeed deleted by printing them out:

console.log('START printing cookies AFTER deleting them');
 angular.forEach($cookies, function (cookie, key) {
    console.log(key + ': ' + $cookies[key] + '\n');
 });
 console.log('DONE printing cookies AFTER deleting them');

Hope this helps



来源:https://stackoverflow.com/questions/20988641/how-to-clear-cookies-in-angular-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!