最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular - $cookies is undefined - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited May 30, 2016 at 8:31 gaurav5430 13.9k8 gold badges70 silver badges127 bronze badges asked Mar 22, 2015 at 15:07 Asaf NevoAsaf Nevo 11.7k23 gold badges85 silver badges154 bronze badges 14
  • which version of angular you are using? – Pankaj Parkar Commented Mar 22, 2015 at 15:10
  • 1 Have you added 'ngCookies' as a dependency for your app? – Or Guz Commented Mar 22, 2015 at 15:11
  • @pankajparkar 1.3.10 – Asaf Nevo Commented Mar 22, 2015 at 15:12
  • @OrGuz yes I did.. adding that to my question – Asaf Nevo Commented Mar 22, 2015 at 15:13
  • 2 seems like $cookie doesn't have setter in angular 1.3.10 code.angularjs/1.3.15/docs/api/ngCookies/service/$cookies while 1.4.0 does code.angularjs/1.4.0-beta.6/docs/api/ngCookies/service/… – Pankaj Parkar Commented Mar 22, 2015 at 15:32
 |  Show 9 more ments

1 Answer 1

Reset to default 5

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.

发布评论

评论列表(0)

  1. 暂无评论