文字
分享

AngularJS: API: ngCookies


ngCookies

ngCookies

The ngCookies module provides a convenient wrapper for reading and writing browser cookies.

参见 $cookies and $cookieStore for usage.

Installation

First include angular-cookies.js in your HTML:

1

2

<code style="box-sizing:border-box;font-family:Menlo, Monaco, Consolas, 'Courier New', monospace;font-size:inherit;padding:0px;color:inherit;background-color:transparent;white-space:pre-wrap;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;"><span style="box-sizing:border-box;color:rgb(51, 51, 51);">    </span><span style="box-sizing:border-box;color:navy;"><script</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);"> </span><span style="box-sizing:border-box;color:teal;">src</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">=</span><span style="box-sizing:border-box;color:rgb(221, 17, 68);">"angular.js"</span><span style="box-sizing:border-box;color:navy;">></span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">

    </span><span style="box-sizing:border-box;color:navy;"><script</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);"> </span><span style="box-sizing:border-box;color:teal;">src</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">=</span><span style="box-sizing:border-box;color:rgb(221, 17, 68);">"angular-cookies.js"</span><span style="box-sizing:border-box;color:navy;">></span></code>

You can download this file from the following places:

  • Google CDN
    例如 //ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-cookies.js
  • Bower
    例如

    1

    <code style="box-sizing:border-box;font-family:Menlo, Monaco, Consolas, 'Courier New', monospace;font-size:inherit;padding:0px;color:inherit;background-color:transparent;white-space:pre-wrap;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;"><span style="box-sizing:border-box;color:rgb(51, 51, 51);">bower install angular</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">-</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">cookies@X</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">.</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">Y</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">.</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">Z</span></code>

  • code.angularjs.org
    例如

    1

    <code style="box-sizing:border-box;font-family:Menlo, Monaco, Consolas, 'Courier New', monospace;font-size:inherit;padding:0px;color:inherit;background-color:transparent;white-space:pre-wrap;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;"><span style="box-sizing:border-box;color:rgb(221, 17, 68);">"//code.angularjs.org/X.Y.Z/angular-cookies.js"</span></code>

where X.Y.Z is the AngularJS version you are running.

Then load the module in your application by adding it as a dependent module:

1

<code style="box-sizing:border-box;font-family:Menlo, Monaco, Consolas, 'Courier New', monospace;font-size:inherit;padding:0px;color:inherit;background-color:transparent;white-space:pre-wrap;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;"><span style="box-sizing:border-box;color:rgb(51, 51, 51);">  angular</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">.</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">module</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">(</span><span style="box-sizing:border-box;color:rgb(221, 17, 68);">'app'</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">,</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);"> </span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">[</span><span style="box-sizing:border-box;color:rgb(221, 17, 68);">'ngCookies'</span><span style="box-sizing:border-box;color:rgb(51, 51, 51);">]);</span></code>

With that you're ready to get started!

模块组件

Service

名称 描述
$cookies

Provides read/write access to browser's cookies.

$cookieStore

Provides a key-value (string-object) storage, that is backed by session cookies. Objects put or retrieved from this storage are automatically serialized or deserialized by angular's toJson/fromJson.