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:
$cookieStore service
$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) });
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!

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
