Angularjs總結(八)$ cookie和$rootscope

來源:互聯網
上載者:User

標籤:


AngularJS 提供了很好的 $cookie 和 $cookieStore API 用來處理 cookies 。
這兩個服務都能夠很好的發揮HTML5 cookies,當HTML5 API可用時瀏覽器會選擇使用HTML5提供的API,如果不可用則預設選擇document.cookies。無論那種方式,你都可以選擇使用相
同的API來進行工作。
現在可以使用put、get 、remove方法操作 cookie , $cookieStore 可以直接儲存物件到cookie中,它會使用angularjs 的 toJson/fromJson 自動序列化和還原序列化
$cookieStore.put("name","my name");
$cookieStore.get("name") == "my name";
$cookieStore.remove("name");
$cookieStore.put("persion", {
name: "my name",
age: 18
});
$scope.person = $cookieStore.get("persion");

使用cookie
$cookies.put(‘sessionSZWC‘, sessionObject);
$cookies.get("sessionSZWC");
$cookies.remove(‘sessionSZWC‘);
$cookies.myFavorite = ‘oatmeal haha‘;

 

scope是html和單個controller之間的橋樑,資料繫結就靠他了。
rootscope是各個controller中scope的橋樑。用rootscope定義的值,可以在各個controller中使用

Angularjs總結(八)$ cookie和$rootscope

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.