如何访问AngularJS饼干? [英] How to access cookies in AngularJS?

查看:304
本文介绍了如何访问AngularJS饼干?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是访问饼干AngularJS方式吗?我已经看到了一个既服务和饼干的模块,但没有实例的引用。

What's the AngularJS way to access cookies? I've seen references to both a service and a module for cookies, but no examples.

是存在的,或者有没有一个规范的AngularJS的方法呢?

Is there, or is there not an AngularJS canonical approach?

推荐答案

这个答案已经被更新,以反映最新的稳定版本angularjs。一个重要的说明是,$的CookieStore是围绕$饼干薄包装。他们是pretty多在同他们只用会话cookie工作。虽然这回答原来的问题,还有其他的解决方案,您不妨考虑,如使用本地存储,或jquery.cookie插件(这将让你更细粒度的控制,做服务器端的cookie。当然,这样做的angularjs意味着你可能会希望将它们包装在一个服务并使用scope.apply角度变化的通知到模型(在某些情况下)。

This answer has been updated to reflect latest stable angularjs version. One important note is that $cookieStore is a thin wrapper surrounding $cookies. They are pretty much the same in that they only work with session cookies. Although, this answers the original question, there are other solutions you may wish to consider such as using localstorage, or jquery.cookie plugin (which would give you more fine-grained control and do serverside cookies. Of course doing so in angularjs means you probably would want to wrap them in a service and use scope.apply to notify angular of changes to models (in some cases).

另外一个说明,那就是在这两个之间有细微的差别获取数据时出因,如果您使用的cookie $存储的值或$的CookieStore上。当然,你真的想用一个或另一个。

One other note and that is that there is a slight difference between the two when pulling data out depending on if you used $cookie to store value or $cookieStore. Of course, you'd really want to use one or the other.

除了添加引用JS文件,你需要注入ngCookies到您的应用程序定义,如:

In addition to adding reference to the js file you need to inject ngCookies into your app definition such as:

angular.module('myApp', ['ngCookies']);

您应该然后去的好。

下面是功能最少的例子,我在那里显示的CookieStore是围绕饼干瘦包装:

Here is a functional minimal example, where I show that cookieStore is a thin wrapper around cookies:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
   <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body ng-controller="MyController">

  <h3>Cookies</h3>
  <pre>{{usingCookies|json}}</pre>
  <h3>Cookie Store</h3>
  <pre>{{usingCookieStore|json}}</pre>

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular-cookies.js"></script>
  <script>
    angular.module('myApp', ['ngCookies']);
    app.controller('MyController',['$scope','$cookies','$cookieStore', 
                       function($scope,$cookies,$cookieStore) {
      var someSessionObj = { 'innerObj' : 'somesessioncookievalue'};

    $cookies.dotobject = someSessionObj;
    $scope.usingCookies = { 'cookies.dotobject' : $cookies.dotobject, "cookieStore.get" : $cookieStore.get('dotobject') };

    $cookieStore.put('obj', someSessionObj);
    $scope.usingCookieStore = { "cookieStore.get" : $cookieStore.get('obj'), 'cookies.dotobject' : $cookies.obj, };
    }
  </script>

</body>
</html>

的步骤是:


  1. 包括angular.js

  2. 包括角cookies.js

  3. ngCookies 到您的应用程序模块(并确保您引用在该模块NG-应用属性)

  4. 添加 $饼干 $的CookieStore 参数控制器

  5. 访问饼干作为使用点一个成员变量(。)运算符
    - 或 -

  6. 使用put访问的CookieStore / get方法

  1. include angular.js
  2. include angular-cookies.js
  3. inject ngCookies into your app module (and make sure you reference that module in the ng-app attribute)
  4. add a $cookies or $cookieStore parameter to the controller
  5. access the cookie as a member variable using the dot (.) operator -- OR --
  6. access cookieStore using put/get methods

这篇关于如何访问AngularJS饼干?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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