我有一个存储在localStorage中的信息列表,这些信息是拆分的。 localStorage的初始值为:free()
。拆分工作正常,但当我刷新页面。值取:FC Girondins de Bordeaux*null*null*OSC Lille
。我该如何解决这个问题?
我的app.js代码:
FC Girondins de Bordeaux null - null OSC Lille undefined - undefined undefined
我的HTML代码:
.controller('myMatchsCtrl', function($scope, $localStorage) {
$scope.$storage= localStorage;
console.log(localStorage);
$scope.nbstore = localStorage.length;
var tabAff = [];
// show localStorage
for ( var i = 0, len = localStorage.length; i < len; ++i ) {
localStorage.getItem( localStorage.key( i ));
tabAff[i] = localStorage[i+1].split("*");
$scope.$storage[i+1] = tabAff[i][0] + " " + tabAff[i][1] + " - " + tabAff[i][2] + " " + tabAff[i][3];
};
答案 0 :(得分:1)
<强>解释强>
当您进行第一次拆分时,您将使用计算值覆盖localStorage。 基本上,这是正在发生的事情:
tabAff[i] = "FC Girondins de Bordeaux*null*null*OSC Lille".split("*")
// tabAff[i] = [ "FC Girondins de Bordeaux", "null", "null", "OSC Lille" ]
由于$ scope。$ storage = localStorage,你正在推出
tabAff[i][0] + " " + tabAff[i][1] + " - " + tabAff[i][2] + " " + tabAff[i][3];
// = "FC Girondins de Bordeaux null - null OSC Lille"
在localStorage中。
当你刷新页面时,你正在做
"FC Girondins de Bordeaux null - null OSC Lille".split("*")
给你
tabAff[i] = [ "FC Girondins de Bordeaux null - null OSC Lille" ]
所以tabAff [i] [0] =“FC Girondins de Bordeaux null - null OSC Lille”, 和tabAff [i] [1] = tabAff [i] [2] = tabAff [i] [3] =未定义
所以
tabAff[i][0] + " " + tabAff[i][1] + " - " + tabAff[i][2] + " " + tabAff[i][3]
给你FC Girondins de Bordeaux null - null OSC Lille undefined - undefined undefined
<强>解决方案强>
不要覆盖localStorage中的值(删除$scope.$storage= localStorage;
)或将计算值(tabAff[i][0] + " " + tabAff[i][1] + " - " + tabAff[i][2] + " " + tabAff[i][3]
)存储在localStorage的另一部分