本文介绍了如何更新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项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 19:55