如何更新 localStorage 项目? [英] How do I update localStorage items?

查看:30
本文介绍了如何更新 localStorage 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,即缓存的对象与正确的数据不符,所以我想我可以将最新版本推送到浏览器缓存中,这样可以解决我的问题.

I'm having a problem where the cached object doesn't resemble the correct data so I figured it I can push up the most uptodate version to the browser cache it will solve my problem.

如何使用新对象更新 localStorage?因此,如果我有一个控制器,它会更新评估.如何将该评估对象推送到 localStorage?

How do you update your localStorage with a new object? So if I had a controller with that had an assessment updated. How can I push that assessment object up to the localStorage?

推荐答案

要使用原生 JavaScript 做到这一点,您可以这样做:

To do that with native JavaScript, you would do something like this:

localStorage.setItem('itemKey', JSON.stringify(yourObject));
var item = JSON.parse(localStorage.getItem('itemKey'));

在 angular 的上下文中,您应该将 localStorage 服务作为 localStorage 的包装器并将其注入您的服务或控制器.或者你可以注入 $window 并直接使用它:$window.localStorage

Within the context of angular, you should make a localStorage service as a wrapper around localStorage and inject it into your service or controller. Or you could inject $window and use it directly off of that like: $window.localStorage

这篇关于如何更新 localStorage 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆