如何清除angular.js中的cookie
目前我正在使用angulajs应用程序。
我想在cookie中存储一些值。 所以我使用angular-cookies-min
脚本为cookie添加一些值
我使用下面的代码将值保存到cookie。
$cookieStore.put("userInfo", userInfo);//userInfo is a array with some values.
现在我要清除cookie? 我该怎么做?
尝试使用此代码删除cookie
$cookieStore.remove("userInfo");
编辑 :由于v1.4 $cookieStore
已被弃用(请参阅文档 ),因此从该版本开始,您应该使用:
$cookies.remove("userInfo");
我找到了一些答案
选项1
delete $cookies["userInfo"]
选项2
.controller('LogoutCtrl', ['$scope', '$cookies', function ($scope,$cookies) { $cookies.userInfo = undefined; }]);
angular.forEach($cookies, function (cookie, key) { if (key.indexOf('NAV-') > -1) { $window.sessionStorage.setItem(key, cookie); delete $cookies[key]; } });
以上代码适用于我,但Chrome检查实用程序的“资源”标签继续显示不会删除Cookie。 我确认通过打印出来确实删除了cookie:
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');
希望这可以帮助