34

I'm trying to use cookies within Angular - here's what I'm trying:

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular-cookies.min.js"></script>

var capApp = angular.module('capApp', ['ngRoute','ui.bootstrap','ngCookies']);

capApp.controller('cookieCtrl', ['$scope','$cookies', function($scope, $cookies) {
  var favoriteCookie = $cookies.get('user_id');
  alert(favoriteCookie);
}]);

I get this error in the console:

TypeError: $cookies.get is not a function

Any ideas where I'm going wrong?

UPDATE

Check which version of Angular you are using for everything - any Angular guys read this, make the version switch in the docs bright green and huge! You simply don't notice it.

StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • because you use `$cookieStore` not `$cookie`, edit: oops my bad, deprecated now edit2: but still, there is no `get()` on `$cookies` https://docs.angularjs.org/api/ngCookies/service/$cookies – Ronnie Mar 10 '15 at 18:15
  • Nope, $cookieStore is deprecated - https://docs.angularjs.org/api/ngCookies/service/$cookieStore – StudioTime Mar 10 '15 at 18:17
  • damn, I'm all over the place, lol. There is a get(). welp leme see... – Ronnie Mar 10 '15 at 18:20
  • Please read this: https://docs.angularjs.org/api/ngCookies/service/$cookies - put writes, get reads – StudioTime Mar 10 '15 at 18:21
  • 8
    ok, I got it. The problem is I was looking at 1.4.0 docs and 1.3.14 docs. The version you are using does NOT have get(). get() was introduced in 1.4.0. I knew I wasn't going crazy – Ronnie Mar 10 '15 at 18:22
  • Aaahhhh - I never even noticed the version switch in the top left corner - Angular seems great but a lot of messing about with versions - code changes every week! – StudioTime Mar 10 '15 at 18:24
  • I'm still using 1.3.x as well. I also started using https://github.com/grevory/angular-local-storage over cookies because I couldn't get cookies to persist over sessions: http://stackoverflow.com/questions/24702432/angularjs-cookies-are-not-persisting – Ronnie Mar 10 '15 at 18:26
  • Thanks, will take a look at that - might close this question – StudioTime Mar 10 '15 at 18:28
  • 6
    I'd leave it open, help some poor soul in the near future – Ronnie Mar 10 '15 at 18:30
  • 2
    Thank god for finding this, I love you all – rosshamish Mar 11 '15 at 01:11
  • 2
    Holy crap. Thanks for leaving this open! – Senica Gonzalez Apr 01 '15 at 22:41
  • 1
    This just saved my sanity. – Jonathan E. Landrum Jul 13 '15 at 20:12

3 Answers3

34

In Angular 1.3.14 you can just use

var favoriteCookie = $cookies[user_id];

See the documentaiton here: Angular Cookies

Rias
  • 1,956
  • 22
  • 33
  • 5
    This doesn't work any more in Angular 1.4.x. From the Angular wesbite: BREAKING CHANGE: $cookies no longer exposes properties that represent the current browser cookie values. Now you must use the get/put/remove/etc. methods as described below. (https://docs.angularjs.org/api/ngCookies/service/$cookies) – LEDfan Apr 15 '15 at 16:11
11

The syntax you have is for version 1.4.x but you are referencing an older version. If you do not want to update to 1.4.x, you can use $cookieStore instead as follows:

$cookieStore.put("key", "value"); 
var value = $cookieStore.get("key");

Alternatively if you are using version 1.3.x or below, you can use $cookies as follows:

$cookies.key = "value";
var value = $cookies.key; 
Nimo
  • 7,984
  • 5
  • 39
  • 41
Maksood
  • 1,180
  • 14
  • 19
2

The following method should work.

var favoriteCookie = $cookies.user_id;
cilerler
  • 9,010
  • 10
  • 56
  • 91