Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: $cookies.remove is not a function

I am getting the error - "$cookies.remove is not a function" - when trying to clear the cookies in my angular application. As far as I can see I am following what the angular docs says, and also what has been said on this forum to remove cookies.

Here is my service which handles this,

app.service('authService', function ($cookies) {

    this.SetUsername = function (username) {
        $cookies.username = username;
    }

    this.GetUsername = function () {
        return $cookies.username;
    }

    this.clearCookie = function(){
        $cookies.remove("username");

    }

});

The get and set functions both work fine, it's just when actually trying to remove the cookie when calling the clear cookie function that i'm getting this issue.

like image 724
user3407039 Avatar asked Sep 27 '22 20:09

user3407039


1 Answers

First, which version of Angular.js are you using? To me it seems that you're using 1.3.x which means that $cookies actually comes from the ngCookies plugin. In that case, $cookies is nothing more than a simple object where writing to creates a new cookie value. To directly quote from the 1.3.x ngCookie docs:

Only a simple Object is exposed and by adding or removing properties to/from this object, new cookies are created/deleted at the end of current $eval. The object's properties can only be strings.

Requires the ngCookies module to be installed.

In case you are using 1.4.x and up your implementation would actually be correct.

like image 108
Sargo Darya Avatar answered Nov 15 '22 11:11

Sargo Darya