设置cookie用put()方法:
$cookies.put(key, value[, options]);
$cookieStore.put(key, value);
例如设置一个cookie,名为“userName”,值为“yangmin”:
//使用$cookies设置cookie $cookies.put('userName', 'yangmin');
//使用$cookieStore设置cookie $cookieStore.put('userName','yangmin');
获取cookie用get()方法:
$cookies.get(key);
$cookieStore.get(key);
例如获取上面设置的“userName”:
$cookies.get(userName);//yangmin
$cookieStore.get("userName"); //yangmin
删除cookie用remove():
$cookies.remove(key[, options]);
$cookieStore.remove(key);
例如删除“userName”
$cookies.remove("userName");
$cookieStore.remove("userName");
$cookies和$cookieStore的区别:
1.$cookies设置的cookie值一般为字符串,$cookieStroe可用于设置字符串、对象、数组等。
$cookies.put("person",{name:"Amy",age:23}); var person = $cookies.get("person"); console.log(person.age);//undefined
$cookieStore.put("person",{name:"Amy",age:23}); var person = $cookieStore.get("person"); console.log(person.age);//23
2.$cookies可设置参数,例如可设置cookie的过期时间。$cookieStore无法设置参数
var expireDate = new Date(); expireDate.setDate(expireDate.getDate() + 1); $cookies.put("userName",“yangmin”,{'expires': expireDate});//“userName”一天后过期