GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Angular操作cookies方法 · Issue #19 · Wscats/articles Closed Labels Descriptionvar setCookie = function(name, value) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); //$cookies[name] = value; }; 这是用Javascript写的方法去设置 var getCookie = function(name) { var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); if (arr = document.cookie.match(reg)) return unescape(arr[2]); else return null; } 这是用Javascript写的方法去读取 <script src="http://code.angularjs.org/1.2.9/angular.min.js"></script> <script src="http://code.angularjs.org/1.2.9/angular-cookies.min.js"></script> 源码是这样的 function(a, b, c) { "use strict"; b.module("ngCookies", ["ng"]).factory("$cookies", ["$rootScope", "$browser", function(a, d) { function e() { var a, e, f, i; for (a in h) k(g[a]) && d.cookies(a, c); for (a in g) e = g[a], b.isString(e) ? e !== h[a] && (d.cookies(a, e), i = !0) : b.isDefined(h[a]) ? g[a] = h[a] : delete g[a]; if (i) { i = !1, f = d.cookies(); for (a in g) g[a] !== f[a] && (k(f[a]) ? delete g[a] : g[a] = f[a], i = !0) } } var f, g = {}, h = {}, i = !1, j = b.copy, k = b.isUndefined; return d.addPollFn(function() { var b = d.cookies(); f != b && (f = b, j(b, h), j(b, g), i && a.$apply()) })(), i = !0, a.$watch(e), g } ]).factory("$cookieStore", ["$cookies", function(a) { return { get: function(c) { var d = a[c]; return d ? b.fromJson(d) : d }, put: function(c, d) { a[c] = b.toJson(d) }, remove: function(b) { delete a[b] } } } ]) }(window, window.angular)
简单的写了几个例子来测试下 <!DOCTYPE html> <html ng-app="WsCatsApp" ng-controller="aswController"> <head> <meta charset="UTF-8"> <title></title> </head> <script src="http://code.angularjs.org/1.2.9/angular.min.js"></script> <script src="http://code.angularjs.org/1.2.9/angular-cookies.min.js"></script> <body> {{title}} </body> <script> var WsCatsApp = angular.module('WsCatsApp', ['ngCookies']); WsCatsApp.controller('aswController', function($cookies, $cookieStore, $scope) { $cookies.name = 'wscats'; $scope.title = "Hello, i'm wscats :)"; $cookieStore.put("skill", "##"); //删除cookies $cookieStore.remove("name"); //设置过期日期 var time = new Date().getTime() + 5000; $cookieStore.put("cookie", "Hello wsscat", { expires: new Date(new Date().getTime() + 5000) }); $cookieStore.put("objCookie", { value: "wsscat cat cat", age: "3", }, { expires: new Date(new Date().getTime() + 5000) }); console.log($cookies); console.log($cookies['objCookie']); }) </script> </html> 其实平时我们这样就可以把自己需要的cookies设置进去 var time = new Date().getTime() + 5000; $cookieStore.put("cookie", "Hello wsscat", { expires: new Date(new Date().getTime() + 5000) }); 我们还可以进行删除等操作 Reacted by THWU ActivityMetadataMetadataAssigneesLabelsTypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |