简体   繁体   中英

AngularJS set localstorage (object json)

How can I change/modify/refresh my localstorage of AngularJS?

My localstorage content is an object json:

//data content: {Id:1,Name:"Juanito"}
localStorageService.set("MyObj",data);

console.log(localStorageService.get("MyObj"));
//SHOW object JSON {Id:1,Name:"Juanito"}

But if I modify a unique key like this:

localStorageService.set("MyObj.Id","otherid"), 

It does not work. How do I fix this?

Try retrieving, then updating and re-setting:

var data = {id: 123}
localStorageService.set("MyObj",data);
var dataFromLS = localStorageService.get("MyObj");
dataFromLS.id = 456;
localStorageService.set("MyObj",data);
console.log(localStorageService.get("MyObj"));
  1. When you change/modify your key name, a brand new object will be created/stored in your service.
  2. If you want to change/alter key's data/value, perform intermediate operations and set it with same key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM