Reputation: 2063
So lets say I want to define an var array=['a','b','c']
How do I pass this array into localStorage ?
Should I just simply write $localStorage.data = array;
?
And also how to check if that array $localStorage.data
is not empty and not undefined?
Upvotes: 0
Views: 1933
Reputation: 594
How do I pass this array into localStorage ? Should I just simply write $localStorage.data = array;?
Yes, you simply put it like this:
$localStorage.data = array
And also how to check if that array $localStorage.data is not empty and not undefined?
Check it like simple variable
if($localStorage.data){
console.log('empty');
} else{
console.log('Data', $localStorage.data);
}
Right from AngularJS homepage: "Unlike other frameworks, there is no need to [...] wrap the model in accessors methods. Just plain old JavaScript here." Now you can enjoy the same benefit while achieving data persistence with Web Storage.
Upvotes: 1
Reputation: 158
You can simply store in localstorage as:
localStorage.setItem("data", JSON.stringify(array));
and later retrieve it as
$scope.data= JSON.parse(localStorage.getItem("data"));
You can check $scope.data by doing
console.log($scope.data);
Upvotes: 2