Improve this Doc View Source $cookies

From Get docs
< Improve this Doc ngCookies‎ | serviceAngularjs/docs/1.8/api/ngcookies/service/$cookies


Improve this Doc View Source $cookies

  1. $cookiesProvider
  2. service in module ngCookies

Overview

Provides read/write access to browser's cookies.

Up until AngularJS 1.3, $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 string

    Id to use for lookup.

    Returns

    string

    Raw cookie value.

  • === getObject(key); ===

    Returns the deserialized value of given cookie key

    Parameters

    Param Type Details
    key string

    Id to use for lookup.

    Returns

    Object

    Deserialized cookie value.

  • === getAll(); ===

    Returns a key value object with all the cookies

    Returns

    Object

    All cookies

  • === put(key, value, [options]); ===

    Sets a value for given cookie key

    Parameters

    Param Type Details
    key string

    Id for the value.

    value string

    Raw value to be stored.

    options

    (optional)

    Object

    Options object. See $cookiesProvider.defaults

  • === putObject(key, value, [options]); ===

    Serializes and sets a value for given cookie key

    Parameters

    Param Type Details
    key string

    Id for the value.

    value Object

    Value to be stored.

    options

    (optional)

    Object

    Options object. See $cookiesProvider.defaults

  • === remove(key, [options]); ===

    Remove given cookie

    Parameters

    Param Type Details
    key string

    Id of the key-value pair to delete.

    options

    (optional)

    Object

    Options 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