我有这样的本地存储:
localStorage.setItem("memberdata", JSON.stringify($scope.products));
$scope.listdata= localStorage.getItem("memberdata");
if($scope.listdata){
$scope.listdata = JSON.parse($scope.listdata)
}else {
localStorage.setItem("memberdata", JSON.stringify($scope.products));
}
$scope.products
包含以下JSON数据:
{
"member_id": 54,
"first_name": "Hima",
"middle_name": "Ganapathy",
"last_name": "Hegde",
"joining_date": "2017-28-04",
"phone": 7411556977,
"password": "1234",
"mail": "shivam@abinfosoft.com",
"system_date": "Apr 28, 2017 2:26:33 PM",
"society_id": 10,
"role": [
{
"role_id": 4,
"name": "User",
"details": "text"
}
],
"associated": [
{
"associated_id": 1,
"associated_name": "Parent",
"primary_member_id": 1
}
],
}
在我看来,如果我发出警报,我就会得到所有JSON数据。
alert(localStorage.getItem("memberdata"));
我试图仅打印会员名称但不起作用。甚至我把listdata放在我控制器的$ scope中。
{{listdata.first_name}} // Working but deleting when page refreshes
答案 0 :(得分:1)
您需要解析JSON
$scope.listdata= JSON.parse(localStorage.getItem("memberdata"))
console.log($scope.listdata.first_name);
修改强>
它不会起作用
$scope.listdata = localStorage.setItem("memberdata", angular.toJson($scope.products));//wrong
在将其保存到localstorage之前,您需要 stringify JSON。
像这样:
localStorage.setItem("memberdata", JSON.stringify($scope.products));
编辑2:
这样做:
$scope.listdata= localStorage.getItem("memberdata");
if($scope.listdata){
$scope.listdata = JSON.parse($scope.listdata)
}else {
localStorage.setItem("memberdata", JSON.stringify($scope.products));
}
答案 1 :(得分:0)
试试这个
var localMemberdata = localStorage.memberdata;
$ rootScope.localMemberdata = JSON.parse(localMemberdata);
在视图中
{{localMemberdata.first_name}}
如果这是正确的,请将其检查为答案,但我确定100%正常工作
答案 2 :(得分:0)
尝试这样的事情:
var localListdata = localStorage.memberdata;
if (localListdata ) {
console.log("true")
$rootScope.localListdata = JSON.parse(localListdat);
} else {
console.log("null")
}
答案 3 :(得分:0)