-7

I am wondering how you would go about incrementing cookies (by one, just to keep in basic.)

var snacks = $cookies.get('fruitSnacks', 1);
snacks ++;`
meskobalazs
  • 15,741
  • 2
  • 40
  • 63
abejdaniels
  • 459
  • 6
  • 15

2 Answers2

3

try this

var snacks = $cookies.get('fruitSnacks') + 1 ;


$cookies.put('fruitSnacks', snacks );
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
2

The Angular Way

The Angular solution would be:

var oldVaue = $cookies.get('my-cookie');
$cookies.put('my-cookie', ++oldValue);

The jQuery Solution

With the jquery-cookie plugin you can handle cookies more easily:

var oldValue = $.cookie('my-cookie');
$.cookie('my-cookie', ++oldValue);

Now the library was superseded by js-cookie, here is the solution with that:

var oldValue = Cookies.get('my-cookie');
Cookies.set('my-cookie', ++oldValue);
meskobalazs
  • 15,741
  • 2
  • 40
  • 63
  • 1
    This all makes sense to me, but not to angular. I am getting an error when I try to use '+++'. If i have them on the left of oldValue, it says "expected expression, got +." If i put them to the right, "expected expression, got )." I am assuming this is happening because of it not recognizing what I am trying to do. I will look into syntax for using that operand.. – abejdaniels Sep 22 '15 at 13:34
  • Well, I screwed that up :) Two pluses is fine. – meskobalazs Sep 22 '15 at 13:37