5

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.

user3407039
  • 1,285
  • 5
  • 25
  • 51
  • 2
    console.log($cookies) ? is there .remove method? – Vasiliy vvscode Vanchuk May 26 '15 at 13:44
  • Did you install angular-cookies? And did you add ngCookies to the dependencies of your app module? Use `console.dir($cookies);` to get the content of the object. Is a remove function defined? You have to open the console of you browser (F12?) to see to output. – Peter Paul Kiefer May 26 '15 at 13:51
  • Did you include angular-cookies.js? https://code.angularjs.org/1.3.15/ – laughingpine May 26 '15 at 13:52
  • When i type that in console it says that $cookies is not defined, but I don't know how that can be the case since I am using it fine for storing values and it is not throwing errors? It is added within my module. and it is included correctly. I have managed to get this working by using delete $cookies["username"] but I'm not sure why this works and the other method does not. – user3407039 May 26 '15 at 14:22

3 Answers3

4

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.

Sargo Darya
  • 569
  • 6
  • 19
  • Hi, I am using v1.3.15 of angular. I thought the purpose of the .remove would be to get rid of a property in the cookie. As I said in the response above, I managed to get this working by using 'delete $cookies["username"];' instead – user3407039 May 26 '15 at 14:27
  • You're correct with your assumption but the .remove method is only available in 1.4.x and up. The API docs from angular make this sometimes hard to realise, but you have an API Version selector in the documentation in the upper left side. If you select 1.3 it will show up. – Sargo Darya May 27 '15 at 07:30
  • 1
    Ah ok, sorry I should have realised you meant that from your initial post. I'll pay closer attention to the version of angular listed when I'm reading the docs in future. – user3407039 May 27 '15 at 10:06
0

Include angular cookies file in your page.

Angular cookies file:-

<script src="https://code.angularjs.org/1.4.4/angular-cookies.js"></script>
vineet
  • 13,832
  • 10
  • 56
  • 76
0

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

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