Я использую ngStorage для хранения данных. Я создал пользовательские объекты, названные игрой, и сохранил их. Он хранится как игра, но когда я обновляю браузер, он возвращается как объект.
var games= $localStorage.games || {};
this.addGame = function(){
var newGame = new game("Game "+games.length);
$localStorage.games = newGame;
}
var game = function(title){
var title=title;
var player1;
var player2;
this.getTitle = function(){
return title;
}
this.setTitle = function(title){
this.title = title;
}
this.getPlayer1 = function(){
return player1;
}
this.setPlayer1 = function(player1){
this.player1 = player1;
}
this.getPlayer2 = function(){
return player2;
}
this.setPlayer1 = function(player1){
this.player2 = player2;
}
}
Когда добавляется новая игра, она сохраняется как игра в LocalStorage, но когда я обновляю bowser и пытаюсь получить игры. Он возвращается как массив объектов, а не игр.
Вы не можете хранить объекты в localStorage. Взлом заключается в том, что вы можете хранить строку и разбирать/разбирать их так.
var game = {
name: 'Super Metroid',
text: 'This is an awesome game!!!'
};
window.localStorage['game'] = JSON.stringify(game);
var superMetroid = JSON.parse(window.localStorage['game'] || '{}');