angular中cookie读写方法

 时间:2026-04-22 19:15:51

1、AngularJS中对cookie的操作封装了一个单独的模块,模块名为ngCookies,若想使用需在页面中先引入angular-cookies.js:

angular中cookie读写方法

2、將ngCookies模块注入到我们自定义的模块的依赖模块中:

angular中cookie读写方法

3、ngCookies模块中有两个cookies读写相关的服务:$cookies和$cookieStroe。无论使用哪种都要先将其注入到控制器中,为了比较二者的区别,都将其注入到控制器中:

angular中cookie读写方法

1、设置cookie用put()方法:

angular中cookie读写方法

angular中cookie读写方法

2、如设置一个cookie,名为“userName”,值为“yangmin”:

angular中cookie读写方法

angular中cookie读写方法

3、获取cookie用get()方法:

angular中cookie读写方法

angular中cookie读写方法

4、如获取上面设置的“userName”:

angular中cookie读写方法

angular中cookie读写方法

5、删除cookie用remove():

angular中cookie读写方法

angular中cookie读写方法

1、1.$cookies设置的cookie值一般为字符串,$cookieStroe可用于设置字符串、对象、数组等。如图

angular中cookie读写方法

angular中cookie读写方法

2、2.$cookies可设置参数,例如可设置cookie的过期时间。$cookieStore无法设置参数

angular中cookie读写方法

  • 珍珠翡翠白玉汤(紫菜豆腐汤)的家常做法
  • 南昌室内设计培训学校、培训机构哪家好?
  • 梦三国之关平打法攻略
  • 名词解释-区际冲突法 BY浙江在线衢州频道
  • 让舌头动起来手抄报
  • 热门搜索
    北京信息科技大学怎么样 供血不足怎么调理 牛肚怎么做好吃 大众汽车怎么样 睾丸痒是怎么回事 用友反记账怎么操作 手机空间不足怎么办 辞职后五险一金怎么处理 疤痕体质怎么办 心脏房颤怎么治疗