【发布时间】:2015-08-08 05:35:37
【问题描述】:
我在尝试清除我的 Angular 应用程序中的 cookie 时收到错误消息 - “$cookies.remove 不是函数”。据我所知,我正在关注 angular 文档所说的内容,以及该论坛上所说的删除 cookie 的内容。
这是我处理这个问题的服务,
app.service('authService', function ($cookies) {
this.SetUsername = function (username) {
$cookies.username = username;
}
this.GetUsername = function () {
return $cookies.username;
}
this.clearCookie = function(){
$cookies.remove("username");
}
});
get 和 set 函数都可以正常工作,只是在调用 clear cookie 函数时实际尝试删除 cookie 时我遇到了这个问题。
【问题讨论】:
-
console.log($cookies) ?有 .remove 方法吗?
-
你安装了 angular-cookies 吗?您是否将 ngCookies 添加到您的应用程序模块的依赖项中?使用
console.dir($cookies);获取对象的内容。是否定义了删除功能?您必须打开浏览器的控制台(F12?)才能看到输出。 -
你是否包含了 angular-cookies.js? code.angularjs.org/1.3.15
-
当我在控制台中输入它时,它说 $cookies 没有定义,但我不知道这是怎么回事,因为我很好地使用它来存储值并且它不会引发错误?它被添加到我的模块中。它包含正确。我已经设法通过使用 delete $cookies["username"] 来完成这项工作,但我不确定为什么这有效,而另一种方法则无效。
标签: angularjs cookies angular-cookies