Angular 1.4.1 $cookies 以 session cookies 结尾

Angular 1.4.1 $cookies ending up as session cookies

我在Angular 1.4.1下设置持久cookie有问题 cookie总是结束(根据Chrome的资源检查,关闭浏览器和更明显的效果重新打开页面)作为 session cookies。

   $scope.HideSystemNotice = function() {
           // set expiry 1 yr in the future - well after System Notice expires
           var now = new Date(),
               exp = new Date(now.getFullYear()+1, now.getMonth(), now.getDate())
           $cookies.put(  'Snotice'+$scope.SystemNoticeInfo[0].snid, // cookiename
                          'dismissed',                               // value
                          { 'expires': exp }                         // expiry
                       );
           $scope.NextSystemNotice();
   };

这与 Whosebug 上其他地方提供的示例基本相同(但我缺少添加到这些会议的代表 :( 因此是单独的问题)

正在设置 cookie - 我可以在 Chrome 的资源视图中看到它们。但是,它们没有有效期(Chrome 显示为 "session")。

我在设置日期时遗漏了什么明显的花絮?谢谢。

编辑....

我在设置到期日时仍然遇到问题。我现在已经升级到 Angular 1.4.7,并尝试在 .config 中使用 $cookiesProvider 默认值没有任何好处

app.config(function($cookiesProvider) {
 var n = new Date();
 $cookiesProvider.defaults = {
    path: '/',
    domain: location.hostname,
    secure: true,
    expires: new Date(n.getFullYear()+1, n.getMonth(), n.getDate())
 };
});

然后在代码的更下方:

    $scope.HideSystemNotice = function() {
        $cookies.put('Snotice'+$scope.SystemNoticeInfo[0].snid, 'dismissed');
        $scope.NextSystemNotice();
    };

而且我仍然只看到正在创建 session 个 cookie。

路径和域是正确的,但是 cookie 没有被标记为安全,所以就像没有应用选项(或者在最新的化身中,默认值)(我似乎不需要设置路径或域名,它们都会被设置)。

谁能指出这两段代码中的错误之处?

遇到了同样的问题,现在找到了解决方案 here。问题是,您不应该创建一个新对象(将 {...} 分配给 defaults),而是像这样编辑 defaults 对象的属性:

app.config(function($cookiesProvider) {
    var n = new Date();
    $cookiesProvider.defaults.path = '/';
    $cookiesProvider.defaults.domain = location.hostname;
    $cookiesProvider.defaults.secure = true;
    $cookiesProvider.defaults.expires = new Date(n.getFullYear()+1, n.getMonth(), n.getDate());
});