Home > Web Front-end > JS Tutorial > body text

AngularJs operating cookies example sharing

小云云
Release: 2018-03-08 15:26:12
Original
1683 people have browsed it

Cookie can be said to be a client-side technology. The program writes some of the user's information to the user's respective browser in the form of a cookie. To put it bluntly, it is the data stored on the user's computer by the website in order to identify the user. The shopping cart function and forum automatic login function of many shopping websites are implemented by cookies.

Operations on cookies include saving, obtaining and deleting.

Angular provides the ngCookies module for users’ convenience and simplicity in operating Cookies. This module provides 2 services, namely:

  1. $cookieStore service

  2. $cookies service

Let’s talk about $cookies first.

$cookies is similar to jQuery.cookie.js, which provides Angular’s ​​method of operating Cookies. Under normal circumstances, JavaScript cannot write objects to Cookies, but Angular provides Method to write objects to Cookie. $cookies provides the following methods:

get(key) 返回一个指定key的cookie值
getObject(key) 返回一个指定key的反序列化cookie值
getAll() 以key-value对象形式返回所有的cookie
put(key,value,[options]) 写入一个key-value的cookie
putObject(key,value,[options])序列化设置一个key-value的Cookie
remove(key,[options]) 移除对应key的cookie
demo:比如用户登录,记住密码的cookie有效期是7天。
  var cookieInfo= {};
  cookieInfo.username = $scope.username;
  cookieInfo.password = $scope.password;
  var expireDate = new Date();
  expireDate.setDate(expireDate.getDate() + 7);//设置cookie保存7天
  $cookies.putObject("user", cookieDate, {'expires': expireDate});
获取也很方便:
$scope.ID = $cookies.getObject("user").username;
$scope.sid = $cookies.getObject("user").password;
简单的只有一个key-value的话用put()比较简单,但是是过期时间是session,关闭浏览器就没有了。
$cookies.put('myFavorite', 'oatmeal');
再说说$cookieStore ,$cookieStore服务是基于后端的Session Cookies,所以写入的时候不能使用options属性,
而且它的过期时间就是session。关闭浏览器了,cookie就失效了。
$cookieStore不可以通过设置default里面的expires来设置过期时间,$cookieStore操作Cookie都是基于Session过期的。
所以上面的例子你如果使用$cookieStore来实现记住用户名和密码的话,关闭浏览器cookies就失效了,无法达到记住n天的这个功能。
$cookieStore.put("user", {
                username:aaa,
                password:123
               expires: new Date(new Date().getDate() + 5000)
            });
Copy after login

The expiration time is invalid! ! ! !

Summary:

The $cookies service provided by the ngCookies module provides great convenience for developers to read and write cookies, and also supports writing and reading objects. Relatively convenient and easy to use. During the development process at that time, we still couldn't write too much data into Cookies. Firstly, because the size of Cookies was limited. On the other hand, writing sensitive data into Cookies was risky in terms of security. Passwords need to be agreed upon with the backend in an encryption method and cannot be stored directly in clear text, which is extremely unsafe. You need to be careful when using cookies. Pay more attention to the path and domain pitfalls, and there will be basically no problems.

Finally: Angular official website has prompted that $cookieStore is obsolete and it is recommended to use $cookies service.

Related recommendations:

javascript Manipulating cookies and correctly using cookie attributes_javascript skills

Implementation code for using jQuery to operate Cookies_jquery

JS operating Cookies includes (reading, adding and deleting)_javascript skills

The above is the detailed content of AngularJs operating cookies example sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!