gsklee / ngStorage

localStorage and sessionStorage done right for AngularJS.
MIT License
2.33k stars 461 forks source link

Unable to Work with JSON Array #189

Closed siddmegadeth closed 8 years ago

siddmegadeth commented 8 years ago

Canot get this below code to work. I am having large set of JSON which has to be added to localstrorage var item = { "name: "someone", "age" : 23, "gender" : "male"

} //Init $scope.storage = $localStorage; $scope.storage.allSelectedTweets = [];

$scope.storage.allSelectedTweets.push(item);

On Page refresh all the values are gone.

Midako commented 8 years ago

try something like this

var item = {
    "name: "someone",
    "age" : 23,
    "gender" : "male"
}

//Init
$scope.storage = $localStorage;
// if allSelectedTweets doesn't exist or length is 0, initial a empty object and fill with item
if(!$scope.storage.allSelectedTweets || $scope.storage.allSelectedTweets.length === 0) {
    $scope.storage.allSelectedTweets = [];
    $scope.storage.allSelectedTweets.push(item);
}

everytime you refresh he set a empty array to "allSelectedTweets". but with a little check he will not update it till its empty or undefined/null

egilkh commented 8 years ago

Midako with the save!

👍