21

目前正在使用 angulajs 应用程序。

我想在 cookie 中存储一些值。所以我使用angular-cookies-min脚本向 cookie 添加一些值

我已经使用下面的代码将值保存到 cookie。

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

现在我想清除cookie?我该怎么做?

4

3 回答 3

58

尝试使用此代码删除 cookie

$cookieStore.remove("userInfo");

编辑:由于 v1.4$cookieStore已被弃用(请参阅文档),因此从该版本开始,您应该使用:

$cookies.remove("userInfo");
于 2014-01-08T06:45:55.007 回答
6

我找到了一些答案

选项1

delete $cookies["userInfo"]

选项 2

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

    $cookies.userInfo = undefined;

}]);
于 2014-01-09T07:01:50.693 回答
2
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');

希望这可以帮助

于 2014-12-04T14:22:05.567 回答