Improve this Doc View Source $cookies
- $cookiesProvider
- service in module ngCookies
Overview
Provides read/write access to browser's cookies.
$cookies exposed properties that represented the current browser cookie values. In version 1.4, this behavior has changed, and $cookies now provides a standard api of getters, setters etc. Requires the ngCookies module to be installed.
Methods
-
get(key);
Returns the value of given cookie key
Parameters
Param Type Details key stringId to use for lookup.
Returns
stringRaw cookie value.
-
getObject(key);
Returns the deserialized value of given cookie key
Parameters
Param Type Details key stringId to use for lookup.
Returns
ObjectDeserialized cookie value.
-
getAll();
Returns a key value object with all the cookies
Returns
ObjectAll cookies
-
put(key, value, [options]);
Sets a value for given cookie key
Parameters
Param Type Details key stringId for the
value.value stringRaw value to be stored.
options (optional)ObjectOptions object. See $cookiesProvider.defaults
-
putObject(key, value, [options]);
Serializes and sets a value for given cookie key
Parameters
Param Type Details key stringId for the
value.value ObjectValue to be stored.
options (optional)ObjectOptions object. See $cookiesProvider.defaults
-
remove(key, [options]);
Remove given cookie
Parameters
Param Type Details key stringId of the key-value pair to delete.
options (optional)ObjectOptions object. See $cookiesProvider.defaults
Example
angular.module('cookiesExample', ['ngCookies'])
.controller('ExampleController', ['$cookies', function($cookies) {
// Retrieving a cookie
var favoriteCookie = $cookies.get('myFavorite');
// Setting a cookie
$cookies.put('myFavorite', 'oatmeal');
}]);
© 2010–2020 Google, Inc.
Licensed under the Creative Commons Attribution License 3.0.
https://code.angularjs.org/1.8.2/docs/api/ngCookies/service/$cookies