设置在单词之间包含空格的 localstorage cookie 服务

Setting localstorage cookie service that contains spaces inbetween words

我可以很好地设置 cookie,但是如果我设置的 cookie 名称是 "Cookie code 1",它会被保存为 "cookie%20code%201"。如何解决单词之间的空格问题?

self.cookie=function(){
    localStorageService.cookie.set("CookieId",decodeURIComponent(self.Id), 100000);

  }

$cookieStore 的 official doc 表示如下:

Provides a key-value (string-object) storage, that is backed by session cookies. Objects put or retrieved from this storage are automatically serialized or deserialized by angular's toJson/fromJson.

意思是,序列化和反序列化(urlEncoded) happens automatically. So i promise you are using this plugin atm https://github.com/grevory/angular-local-storage#cookieget,也就是serialize/unserialize。只要用localStorageService.cookie.get(key);检查一下回调就可以了。

myApp.controller('MainCtrl', function($scope, localStorageService) {
  //...
  function setAndGetItem(key) {
    localStorageService.cookie.set(), "Its a cookie"); // persisted as "Its%20a%20cookie"
    console.log(localStorageService.cookie.get(key)); // returns "Its a cookie"
  }
  //...
});