RaV RaV - 7 months ago 16
Javascript Question

Restoring form from LocalStorage when page refresh

<div ng-repeat="filterSection in filterSections">
<h4>{{ filterSection.title }}</h4>
<div class="checkbox" ng-click="loaderStart()" ng-if="filterSection.control == 'checkbox'" ng-repeat="option in filterSection.options">
<label>
<input type="checkbox" ng-model="searchParameters[filterSection.key][option.key]" value="{{ option.key }}" checked><span></span> {{ option.value }}
</label>
</div>
</div>


I can access it with
$scope.searchParameters
.

I can store it using
localStorage.setItem('search', JSON.stringify(cleanedParameters));


I can access store with
JSON.parse(localStorage.getItem("search"));


But I have no idea how could I restore it when page refresh. Is there some function or example of directive which I could use? I couldn't really find anything similar to my problem.

Answer

You can try as shown below.Just create an array and push the values into that.Then you can access that array values as you wish through a loop.

var a = [];
a.push(JSON.parse(localStorage.getItem('search')));