AngularJS $ cookies.get()总是返回undefined [英] AngularJS $cookies.get() always returns undefined

查看:1533
本文介绍了AngularJS $ cookies.get()总是返回undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AngularJS 1.4.0和 $饼干服务。下面code总是会打印出一个空对象:

I'm using AngularJS 1.4.0 and the $cookies service. The following code will always print out an empty object:

(function() {
    var app = angular.module("user-cookies-service", []);

    app.service('UserCookiesService', ["$cookies", function($cookies) {
        $cookies.put("Hello", "World");
        console.log($cookies.getAll());
    }]);
})();

我试过:


  • 使用AngulerJS 1.3.15和 $的CookieStore ,其中cookie时刷新浏览器(但它至少创建的cookie)不会持续。

  • 添加一个截止日期。

  • 确保Cookies是在我的浏览器(Chrome)启用。

  • 试图普通的旧JS,仍然无法正常工作。

  • Using AngulerJS 1.3.15 and $cookieStore where cookies don't persist when the browser is refreshed (but it at least created the cookies).
  • Adding an expiry date.
  • Making sure Cookies are enabled in my browser (Chrome).
  • Trying plain old JS, which still doesn't work.

我一直没能在别处找到类似的问题。

I haven't been able to find a similar issue anywhere.

感谢您。

推荐答案

对于任何人谁在这个问题绊...

For anyone who stumbles upon this problem...

根据预设,Chrome不允许本地文件的Cookie。你必须用运行 - 使文件饼干标志

By default, Chrome doesn't allow cookies for local files. You have to run it with the --enable-file-cookies flag.

确认Chrome关闭和打开Windows命令提示符/终端,运行以下命令:

Make sure Chrome is closed and open the Windows Command Prompt/Terminal and run the following command:

<path-to-chrome>/chrome.exe --enable-file-cookies

这将让你把/获取饼干本地。

This will allow you to put/get cookies locally.

干杯!

这篇关于AngularJS $ cookies.get()总是返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆