Search code examples
javascriptangularjslocal-storagelocal

How do I update localStorage items?


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.

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?


Solution

  • 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'));
    

    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