简体   繁体   中英

How to delete ngCookie (angular-cookies)

I created a cookie called 'stateCookie' when I start my app. As I change state I add variables into that cookie. When I logout, I want this cookie to be completely destroyed, or empty.

On their docs it's just remove() I've done that but the cookie still exists. ie: $cookies.remove('stateCookie');

After logout and login: >>> 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.

$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();
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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