简体   繁体   English

角度更新本地存储值

[英]Angular update Local Storage value

I was looking at some examples of angular with local storage as i inherited a project that is using it. 当我继承了使用它的项目时,我正在查看一些有关本地存储角度的示例。

This is what I am seeing 这就是我所看到的

var order;
order = userCache.get('order');
console.log('order from cache', order);

Chrome dev tools json output Chrome开发人员工具json输出

{
  "lineItems": [
    {
      "serviceLocation": {
        "verified": true,
        "formattedAddress": "2110 El Pinto Rd, Sullivan City, TX 78595",
        "address1": "2110 El Pinto Rd",
        "city": "Sullivan City",
        "state": "TX",
        "zip": "78595"
      },
      "additionalLocations": [],
      "services": {
        "electric": {
          "selectedProgramId": null,
          "utilityAndProgram": null,
          "serviceProvider": null
        },
        "naturalGas": {
          "selectedProgramId": null,
          "utilityAndProgram": null,
          "serviceProvider": null
        }
      }
    }
  ],
  "authorizedParty": {
    "firstName": "sdfsad",
    "lastName": "asdgfdsaf",
    "phone": "(222) 222-2222"
  },
  "contactPreference": "phone",
  "signature": null,
  "isNew": false
}

What I want to do is just UPDATE the address field by adding in some more data. 我想做的就是通过添加更多数据来更新address字段。

So first I was just trying to read some of that data 所以首先我只是想读取一些数据

Since i put in javascript a object called order , based on the json object structure, shouldn't I be able to just do ? 由于我在javascript中放置了一个基于json对象结构的名为order的对象,所以我不应该这样做吗?

**UPDATE**

Ok, i figured this part out.. array [0] 好的,我想出了这部分..数组[0]

var orderAddress = order.lineItems[0].serviceLocation.address1;
console.log(orderAddress);    

And then How can I update that value ? 然后如何更新该值? setItem or set ? setItemset

Update on userCache it looks like this 在userCache上更新看起来像这样

.service('userCache', function (CacheFactory) {
// NOTE : Used to store specific information about this user, gets cleared on logout.
return CacheFactory.createCache('userCache', {
  maxAge: 30 * 24 * 60 * 60 * 1000, // Items added to this cache expire after 30 days.
  deleteOnExpire: 'aggressive',
  storageMode: 'localStorage'
});

}); });

When you have to update data in your local storage, you usually need to: 当您必须更新本地存储中的数据时,通常需要:

  1. Parse data from local storage 解析本地存储中的数据
  2. Update the entry you want to be updated 更新您要更新的条目
  3. Save data in local storage 将数据保存在本地存储中

This is due to the string nature of local storage: when you save objects, arrays or whatever you want, the are converted to string since Local Storage is a string storage. 这是由于本地存储的字符串性质所致:当您保存对象,数组或所需的任何对象时,由于本地存储是字符串存储,因此它们将转换为字符串。

But since you're a lucky boy and you're using Angular, there's plenty of modules you can use to manage local storage without the need to worry about stringification. 但是,由于您是个幸运儿并且正在使用Angular,因此可以使用许多模块来管理本地存储,而不必担心字符串化。

My personal favourite is ngStorage 我个人最喜欢的是ngStorage

--- EDIT --- - 编辑 -

Well, with angular-cache you simply follow the steps above: 好吧,使用angular-cache您只需按照以下步骤操作:

  1. var data = JSON.parse(userCache.get('order'))
  2. data.lineItems[0].serviceLocation.address = *whatever*
  3. userCache.put('order', data)

First Get the value from Local Storage Updte It and agian set the updated JSON with the key It will update. 首先从Local Storage Updte中获取值,然后用密钥It will update来设置更新的JSON。

In Pure javascript , It will be like. 在纯javascript中,它将像。

`var storage = {}; `var storage = {};

storage.setUpdateData = function(updatedJson) { window.localStorage.setItem("keyName",updatedJson); storage.setUpdateData = function(updatedJson){window.localStorage.setItem(“ keyName”,updatedJson); }` }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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