2 * @license AngularJS v1.3.20
3 * (c) 2010-2014 Google, Inc. http://angularjs.org
6 (function(window, angular, undefined) {'use strict';
15 * The `ngCookies` module provides a convenient wrapper for reading and writing browser cookies.
18 * <div doc-module-components="ngCookies"></div>
20 * See {@link ngCookies.$cookies `$cookies`} and
21 * {@link ngCookies.$cookieStore `$cookieStore`} for usage.
25 angular.module('ngCookies', ['ng']).
31 * Provides read/write access to browser's cookies.
33 * Only a simple Object is exposed and by adding or removing properties to/from this object, new
34 * cookies are created/deleted at the end of current $eval.
35 * The object's properties can only be strings.
37 * Requires the {@link ngCookies `ngCookies`} module to be installed.
42 * angular.module('cookiesExample', ['ngCookies'])
43 * .controller('ExampleController', ['$cookies', function($cookies) {
44 * // Retrieving a cookie
45 * var favoriteCookie = $cookies.myFavorite;
47 * $cookies.myFavorite = 'oatmeal';
51 factory('$cookies', ['$rootScope', '$browser', function($rootScope, $browser) {
57 isUndefined = angular.isUndefined;
59 //creates a poller fn that copies all cookies from the $browser to service & inits the service
60 $browser.addPollFn(function() {
61 var currentCookies = $browser.cookies();
62 if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl
63 lastBrowserCookies = currentCookies;
64 copy(currentCookies, lastCookies);
65 copy(currentCookies, cookies);
66 if (runEval) $rootScope.$apply();
72 //at the end of each eval, push cookies
73 //TODO: this should happen before the "delayed" watches fire, because if some cookies are not
74 // strings or browser refuses to store some cookies, we update the model in the push fn.
75 $rootScope.$watch(push);
81 * Pushes all the cookies from the service to the browser and verifies if all cookies were
90 //delete any cookies deleted in $cookies
91 for (name in lastCookies) {
92 if (isUndefined(cookies[name])) {
93 $browser.cookies(name, undefined);
94 delete lastCookies[name];
98 //update all cookies updated in $cookies
99 for (name in cookies) {
100 value = cookies[name];
101 if (!angular.isString(value)) {
103 cookies[name] = value;
105 if (value !== lastCookies[name]) {
106 $browser.cookies(name, value);
107 lastCookies[name] = value;
112 //verify what was actually stored
114 browserCookies = $browser.cookies();
116 for (name in cookies) {
117 if (cookies[name] !== browserCookies[name]) {
118 //delete or reset all cookies that the browser dropped from $cookies
119 if (isUndefined(browserCookies[name])) {
120 delete cookies[name];
121 delete lastCookies[name];
123 cookies[name] = lastCookies[name] = browserCookies[name];
138 * Provides a key-value (string-object) storage, that is backed by session cookies.
139 * Objects put or retrieved from this storage are automatically serialized or
140 * deserialized by angular's toJson/fromJson.
142 * Requires the {@link ngCookies `ngCookies`} module to be installed.
147 * angular.module('cookieStoreExample', ['ngCookies'])
148 * .controller('ExampleController', ['$cookieStore', function($cookieStore) {
150 * $cookieStore.put('myFavorite','oatmeal');
152 * var favoriteCookie = $cookieStore.get('myFavorite');
153 * // Removing a cookie
154 * $cookieStore.remove('myFavorite');
158 factory('$cookieStore', ['$cookies', function($cookies) {
163 * @name $cookieStore#get
166 * Returns the value of given cookie key
168 * @param {string} key Id to use for lookup.
169 * @returns {Object} Deserialized cookie value.
172 var value = $cookies[key];
173 return value ? angular.fromJson(value) : value;
178 * @name $cookieStore#put
181 * Sets a value for given cookie key
183 * @param {string} key Id for the `value`.
184 * @param {Object} value Value to be stored.
186 put: function(key, value) {
187 $cookies[key] = angular.toJson(value);
192 * @name $cookieStore#remove
195 * Remove given cookie
197 * @param {string} key Id of the key-value pair to delete.
199 remove: function(key) {
200 delete $cookies[key];
207 })(window, window.angular);