Asaf Nevo Asaf Nevo - 6 months ago 219
AngularJS Question

Angular - $cookies is undefined

I'm trying to user Angular cookies in my controller but for some reason it is always undefined:

MyApp.controller('ValidationController', ["$cookies", function($cookies) {
debugger; //checking $cookies here return an object
$cookies.put("test","test"); //trying to add value to the cookie here return TypeError: undefined is not a function
alert(JSON.stringify($cookies));

}]);


var MyApp = angular.module("MyApp", [
"ui.router",
"ui.bootstrap",
"oc.lazyLoad",
"ngSanitize",
"ngCookies"
]);


This is the full stack error:

TypeError: undefined is not a function
at new <anonymous> (CookiesController.js:6)
at Object.e [as invoke] (angular.min.js:37)
at $get.z.instance (angular.min.js:76)
at angular.min.js:59
at s (angular.min.js:7)
at v (angular.min.js:59)
at g (angular.min.js:52)
at angular.min.js:51
at angular.min.js:17
at l.$get.l.$eval (angular.min.js:126)


any ideas?

Answer

Seems like $cookies doesn't have setter in angular 1.3.10 while angular 1.4.0 does provided, Please refer their documents.

You could solve your problem by 2 ways.

  1. If you want to stay with angular 1.3.10 then use $cookieStore object instead of $cookies
  2. Do upgrade angular to version 1.4.0.beta.6 latest version

Hope this could help you, Thanks.