5 * @name angular.module.ngCookies
9 angular.module('ngCookies', ['ng']).
12 * @name angular.module.ng.$cookies
16 * Provides read/write access to browser's cookies.
18 * Only a simple Object is exposed and by adding or removing properties to/from
19 * this object, new cookies are created/deleted at the end of current $eval.
23 factory('$cookies', ['$rootScope', '$browser', function ($rootScope, $browser) {
29 isUndefined = angular.isUndefined;
31 //creates a poller fn that copies all cookies from the $browser to service & inits the service
32 $browser.addPollFn(function() {
33 var currentCookies = $browser.cookies();
34 if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl
35 lastBrowserCookies = currentCookies;
36 copy(currentCookies, lastCookies);
37 copy(currentCookies, cookies);
38 if (runEval) $rootScope.$apply();
44 //at the end of each eval, push cookies
45 //TODO: this should happen before the "delayed" watches fire, because if some cookies are not
46 // strings or browser refuses to store some cookies, we update the model in the push fn.
47 $rootScope.$watch(push);
53 * Pushes all the cookies from the service to the browser and verifies if all cookies were stored.
61 //delete any cookies deleted in $cookies
62 for (name in lastCookies) {
63 if (isUndefined(cookies[name])) {
64 $browser.cookies(name, undefined);
68 //update all cookies updated in $cookies
69 for(name in cookies) {
70 value = cookies[name];
71 if (!angular.isString(value)) {
72 if (angular.isDefined(lastCookies[name])) {
73 cookies[name] = lastCookies[name];
77 } else if (value !== lastCookies[name]) {
78 $browser.cookies(name, value);
83 //verify what was actually stored
86 browserCookies = $browser.cookies();
88 for (name in cookies) {
89 if (cookies[name] !== browserCookies[name]) {
90 //delete or reset all cookies that the browser dropped from $cookies
91 if (isUndefined(browserCookies[name])) {
94 cookies[name] = browserCookies[name];
106 * @name angular.module.ng.$cookieStore
110 * Provides a key-value (string-object) storage, that is backed by session cookies.
111 * Objects put or retrieved from this storage are automatically serialized or
112 * deserialized by angular's toJson/fromJson.
115 factory('$cookieStore', ['$cookies', function($cookies) {
120 * @name angular.module.ng.$cookieStore#get
121 * @methodOf angular.module.ng.$cookieStore
124 * Returns the value of given cookie key
126 * @param {string} key Id to use for lookup.
127 * @returns {Object} Deserialized cookie value.
130 return angular.fromJson($cookies[key]);
135 * @name angular.module.ng.$cookieStore#put
136 * @methodOf angular.module.ng.$cookieStore
139 * Sets a value for given cookie key
141 * @param {string} key Id for the `value`.
142 * @param {Object} value Value to be stored.
144 put: function(key, value) {
145 $cookies[key] = angular.toJson(value);
150 * @name angular.module.ng.$cookieStore#remove
151 * @methodOf angular.module.ng.$cookieStore
154 * Remove given cookie
156 * @param {string} key Id of the key-value pair to delete.
158 remove: function(key) {
159 delete $cookies[key];