我在商店中遇到问题localStorage JSON只为其商店提供一个数据。当我添加更多数据时,它只显示一个。请帮帮我!
存储值
var fav_game = [{
id : "1212",
name : "Cricket"
}];
存储LocalStorage数据
window.localStorage['favgame'] = JSON.stringify(fav_game);
var favgame = JSON.parse(window.localStorage['favgame'] || '{}');
获取本地存储项目:
$scope.favgame = JSON.parse(localStorage.getItem('fav_game')) || [];
console.log(localStorage['favgame']);
答案 0 :(得分:0)
var fav_game = [{ id : "1212", name : "Cricket" }];
window.localStorage['favgame'] = JSON.stringify(fav_game);
function addValue(key,val){
temp = [];
tmp = localStorage.getItem(key);
if(tmp != null){
temp.push(JSON.parse(tmp));
temp.push(val);
window.localStorage['favgame'] = JSON.stringify(temp)
}
}
addValue('favgame','here your valid data')