简体   繁体   English

如何删除ngCookie(angular-cookies)

[英]How to delete ngCookie (angular-cookies)

I created a cookie called 'stateCookie' when I start my app. 启动我的应用程序时,我创建了一个名为“ stateCookie”的cookie。 As I change state I add variables into that cookie. 在更改状态时,我将变量添加到该Cookie中。 When I logout, I want this cookie to be completely destroyed, or empty. 注销时,我希望此Cookie被完全销毁或为空。

On their docs it's just remove() I've done that but the cookie still exists. 在他们的文档中,只是完成了remove() ,但cookie仍然存在。 ie: $cookies.remove('stateCookie'); 即: $cookies.remove('stateCookie');

After logout and login: >>> refreshCookie {"ticker":"SPY","term_id_1":5112166,"term_id_2":30111615,"term_id_3":13064871} 注销并登录后: >>> refreshCookie {"ticker":"SPY","term_id_1":5112166,"term_id_2":30111615,"term_id_3":13064871}

在此处输入图片说明


this.logout = () => AuthFactory.webservice_logout().then((res) => {
    // $cookies.remove('stateCookie');
    angular.forEach($cookies, function(value, key) {
        $cookies.remove(key);
    });

    // console.log('$cookies', $cookies);

    // $cookies.map((value, key) => {
    //  $cookies.remove(key);
    // });

    const refreshCookie = $cookies.get('stateCookie');
    console.log('!!!refreshCookie', refreshCookie);
    $state.go('login', {}).then(() => {
        $state.reload();
    });
});

Hack fix 破解修复

I'm able to clear the cookie by calling window.reload after going back to the login state. 返回login状态后,我可以通过调用window.reload清除cookie。

$state.go('login', {}).then(() => window.location.reload(true));

Try this: 尝试这个:

this.logout = () => AuthFactory.webservice_logout().then((res) => {
    // $cookies.remove('stateCookie');
    var cookies = $cookies.getAll();
    console.log(cookies);
    angular.forEach(cookies, function(value, key) {
        $cookies.remove(key);
    });

    // console.log('$cookies', $cookies);

    // $cookies.map((value, key) => {
    //  $cookies.remove(key);
    // });

    const refreshCookie = $cookies.get('stateCookie');
    console.log('!!!refreshCookie', refreshCookie);
    $state.go('login', {}).then(() => {
        $state.reload();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM