angular中cookie读写方法

2025-10-26 03:22:04

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读写方法

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢