欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angularjs cookie 操作實(shí)例詳解

 更新時(shí)間:2017年09月27日 16:50:12   作者:wolfy  
本文給大家分享Angularjs cookie 操作實(shí)例詳解,demo案例分析,感興趣的朋友參考下吧

摘要

現(xiàn)在很多app采用內(nèi)嵌h5的方式進(jìn)行開發(fā),有些數(shù)據(jù)會(huì)存在webveiw的cookie中,那么如果使用angularjs開發(fā)單頁應(yīng)用,就需要用到angularjs的cookie操作。這里提供一個(gè)簡單的學(xué)習(xí)demo。方便快速上手。

一個(gè)例子

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myapp">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title></title>
  <script src="Scripts/angular.js"></script>
  <script src="Scripts/angular-cookies.js"></script>
  <script>
    angular.module("myapp", ["ngCookies"]).controller("TestController", function ($cookies) {     // Put cookie
      $cookies.put('mytest', 'mytest');
      // Get cookie
      var mytestCookie = $cookies.get('mytest');
      console.log(mytestCookie)
      // Removing a cookie
      // $cookie.remove('mytest');
      console.log($cookies.get('mytest'));
    });
  </script>
</head>
<body ng-controller="TestController">
</body>
</html>

測(cè)試結(jié)果

可以看到上面結(jié)果設(shè)置成功了,但過期時(shí)間是session,這種cookie是跟當(dāng)前會(huì)話相同了,也就是關(guān)閉瀏覽器之后就會(huì)消失,這是因?yàn)槲覀儧]有設(shè)置cookie的過期時(shí)間造成了,可以通過下面的方式設(shè)置過期時(shí)間。

 var expireDate = new Date();
      expireDate.setDate(expireDate.getDate() + 30);//設(shè)置cookie保存30天
      // Put cookie
      $cookies.put('mytest', 'mytest', { 'expires': expireDate });

總結(jié)

這里需要注意,在網(wǎng)上看到很多實(shí)用$cookieStore的,確實(shí)可以設(shè)置成功,但設(shè)置過期時(shí)間的時(shí)候會(huì)失效,建議采用$cookies

以上所述是小編給大家介紹的Angularjs cookie 操作實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論