0

I'm working on a page with some FAQs, and i'm using angular localstorage to save data. Then i'm uploading all my files to a server, to share with other people. When I do this I loose the data, and get it.It's the first time i'm uploading, so it can't be there. Then I insert some data, and it works fine, even if i close browser and open it again, my data is there.

But then when I upload again my .html file, to the server, i loose it again. It's the same url, same html page, but some how the data in angular localstorage kind a gets reseted.

I'm new at angular, and i'm using localstorage for the first time. I've search for this issue but have not found any tips.

How can i keep data in localstorage?

My code in angular module is:

var app = angular.module('WikiCTTXApp',['ngSanitize', 'ngStorage']);

app.controller('InfoInterController',function($scope, $localStorage,$sessionStorage){

 $scope.$storage = $localStorage; $scope.InfoInter = $localStorage.InfoInter; $scope.newInfoInter = {}; $scope.addInfoInter=function(){ /* AD(added later): I added this line to test and initialize the variable and my issue disapeard */ if (typeof($scope.InfoInter) == 'undefined'){$scope.InfoInter=[]}; $scope.InfoInter.push($scope.newInfoInter); $scope.newInfoInter = {}; $scope.save(); }; $scope.remove = function(item) { var index = $scope.InfoInter.indexOf(item); $scope.InfoInter.splice(item, 1); }; $scope.save = function() { $localStorage.InfoInter = $scope.InfoInter; }; $scope.load = function() { $scope.InfoInter = $localStorage.InfoInter; }; }); 
2
  • please share some code where you get and set the localStorage data. Commented Aug 15, 2017 at 9:08
  • just added my code in angular module Commented Aug 15, 2017 at 9:15

1 Answer 1

0

I've not encountered that specific case, but LocalStorage is an HTML5 feature used by Angular, and not a feature of Angular itself. It is therefore dependent on the implementation of the browser being used and the users settings - you do not have much control over whether (or how long) the storage will be persisted.

As a general rule you should never assume that it will be there, and you should always write your code so that it handles the case where your data does not exist.

Sign up to request clarification or add additional context in comments.

6 Comments

ok, what would be your advice as to keep my data? I just need a simple way to save my FAQs in the server and load them, automatically, without user intervention like download file or something llike that.. thanks
If you're storing data on the server, then localstorage (which is client based) is definitely the wrong place. I'd suggest you look at storing them in a json file that you can load into your client app - or serve them from a backend API.
But I have a form on the page, to allow users to add new FAQs. Will I b able to save them to a Json file on the server without user intervention? I've tried to do it but only could find some examples that implied downloading a json file to a "save as" location. thank you
No you wont be able to do that - you will have to go for the API route if you want to allow users to modify the FAQs.
I 've been able to fix my issue of not assuming previous Localstorage data, after the upload of new version of the same HTML page no server. I seemed to have an issue initializing an array variable inside my angular controller. I've fix that, and stop loosing previous data! I'll update my code above.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.