2 * @license AngularJS v1.3.6-build.3675+sha.facfec9
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);
97 //update all cookies updated in $cookies
98 for (name in cookies) {
99 value = cookies[name];
100 if (!angular.isString(value)) {
102 cookies[name] = value;
104 if (value !== lastCookies[name]) {
105 $browser.cookies(name, value);
110 //verify what was actually stored
113 browserCookies = $browser.cookies();
115 for (name in cookies) {
116 if (cookies[name] !== browserCookies[name]) {
117 //delete or reset all cookies that the browser dropped from $cookies
118 if (isUndefined(browserCookies[name])) {
119 delete cookies[name];
121 cookies[name] = browserCookies[name];
137 * Provides a key-value (string-object) storage, that is backed by session cookies.
138 * Objects put or retrieved from this storage are automatically serialized or
139 * deserialized by angular's toJson/fromJson.
141 * Requires the {@link ngCookies `ngCookies`} module to be installed.
146 * angular.module('cookieStoreExample', ['ngCookies'])
147 * .controller('ExampleController', ['$cookieStore', function($cookieStore) {
149 * $cookieStore.put('myFavorite','oatmeal');
151 * var favoriteCookie = $cookieStore.get('myFavorite');
152 * // Removing a cookie
153 * $cookieStore.remove('myFavorite');
157 factory('$cookieStore', ['$cookies', function($cookies) {
162 * @name $cookieStore#get
165 * Returns the value of given cookie key
167 * @param {string} key Id to use for lookup.
168 * @returns {Object} Deserialized cookie value.
171 var value = $cookies[key];
172 return value ? angular.fromJson(value) : value;
177 * @name $cookieStore#put
180 * Sets a value for given cookie key
182 * @param {string} key Id for the `value`.
183 * @param {Object} value Value to be stored.
185 put: function(key, value) {
186 $cookies[key] = angular.toJson(value);
191 * @name $cookieStore#remove
194 * Remove given cookie
196 * @param {string} key Id of the key-value pair to delete.
198 remove: function(key) {
199 delete $cookies[key];
206 })(window, window.angular);