我试图使用注销函数清除域中的所有cookies
,域为localhost:8080
当我试图从cookie
中清除dashboard
时,cookie
将被成功清除。但是,当我从editprofile
导航到dashboard
并试图清除cookie时,cookie不会被清除,因为cookie变量{{user.username}}
仍然显示在索引上。
以下是我的尝试:
$rootScope.logout = function() {
delete $rootScope.user;
$cookies.remove('token');
//$location.path("/");
$(location).attr('href', '/index');
return;
};
下面是在索引页面中定义的内容
<a ng-show="user" class="item active" href="/dashboard">welcome {{user.username}}</a>
发布于 2018-05-30 11:19:43
要删除您应该使用的cookies
,
$cookies.remove("cookieName")
因为您的不是single page application
,所以您应该从每一页中获取remove
cookies
。
$cookies.remove(v, {path: '/yourpage'});
这样您就可以删除所有的cookies
var cookies = $cookies.getAll();
angular.forEach(Object.keys(cookies), function (v, k) {
$cookies.remove(v, {path: '/yourpage'});
});
代码:
$rootScope.logout = function() {
delete $rootScope.user;
var cookies = $cookies.getAll();
angular.forEach(Object.keys(cookies), function (v, k) {
$cookies.remove(v, {path: '/index'});
$cookies.remove(v, {path: '/editprofile'});
$cookies.remove(v, {path: '/'});
});
$cookies.remove("token", {path: '/index'});
$cookies.remove("token", {path: '/editprofile'});
$cookies.remove("token", {path: '/'});
$(location).attr('href', '/index');
return;
};
https://stackoverflow.com/questions/50603193
复制相似问题