Angular ngStorage自定义对象

时间:2015-11-26 17:37:35

标签: javascript angularjs ionic ng-storage

我正在使用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并试图获取游戏时。它以对象数组而非游戏形式返回。

1 个答案:

答案 0 :(得分:1)

您无法在localStorage中存储对象。黑客是你可以存储字符串并解析/解析它们。

'G'