user3407039
user3407039

Reputation: 1335

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.

Upvotes: 5

Views: 4809

Answers (3)

Try this while logout it will clear all cookies at a time

app.controller('controllername', function ($cookies) {
    $cookies.remove();})

Upvotes: 0

vineet
vineet

Reputation: 14266

Include angular cookies file in your page.

Angular cookies file:-

<script src="https://code.angularjs.org/1.4.4/angular-cookies.js"></script>

Upvotes: 0

That White Eyebrow Guy
That White Eyebrow Guy

Reputation: 569

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.

Upvotes: 4

Related Questions