【问题标题】:AngularJS adding array to cookie storeAngularJS将数组添加到cookie存储
【发布时间】:2014-04-23 15:14:29
【问题描述】:

sn-p如下

function CartForm($scope,$cookieStore) {
$scope.invoice = {
    items: [{
        qty: 10,
        description: 'item',
        cost: 9.95}]
};
//want to add the above array to cookie store here 
}

我怎样才能把上面的数组放到cookiestore中,然后访问它并绑定它?

【问题讨论】:

  • Put:$cookieStore.put('invoice', $scope.invoice);Get:return $cookieStore.get('invoice');
  • 你好@MohammadSepahvand 是的,我试过了。检查这个 JSFiddle jsfiddle.net/slav123/75m7e/3 。代码是从中获取的。当我将 $cookieStore 添加到 CartForm 时,它停止运行。我尝试添加 angular cookie 的外部资源,它仍然没有工作。
  • 看我的回答,它有一个工作的plunk。
  • 是的,它现在工作正常。只需将其放在本地主机上。非常感谢:)

标签: arrays angularjs cookies


【解决方案1】:

在 cookie 中放入和获取数据的标准方法是这样的:

输入:$cookieStore.put('invoice', $scope.invoice);

获取:return $cookieStore.get('invoice');

这将需要在您的应用程序中引用 ngCookies,以及外部 angular-cookies 脚本。查看this plunk,查看控制台中记录的cookie。

【讨论】:

  • 非常感谢!它在 plunk 上效果很好。但是当我在我的系统上运行它时似乎不起作用。控制台返回 undefined 。
  • 在包含 angular 的主脚本之后,是否包含了 angular-cookies 脚本?当然,这两个都必须在您的应用程序脚本之前。
  • 是的,cookie 脚本在 angularjs 脚本之后和主应用程序脚本之前。它在 plunk 上完美运行。
  • 您在控制台中看到任何错误吗?我认为你应该升级你的角度版本,使用 1.2+。
  • 我在 angular cookie 之前使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-28
  • 2013-07-03
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多