如何在localStorage中存储对象数组?

时间:2022-05-29 17:11:44

I have 3 radio buttons say x, y and z. On click on x, data is loaded and so on..

我有3个单选按钮说x,y和z。点击x,加载数据等等。

Now in localStorage, I want to set data for x, y and z differently and get it.

现在在localStorage中,我想以不同方式设置x,y和z的数据并获取它。

Here, I'm giving a sample of the program.

在这里,我给出了一个程序样本。

Javascript:

使用Javascript:

var setInLocalStorage = function(){
    //on click of  x radio and getting data from model and called 
    // from one  function
    var x = {
        t = model.x.t;
        d = model.x.d;
    }
    localStorage.setItem('x' ,JSON.stringyfy(x));
}

var getFromLocalStorage = function() { 
    var obj = JSON.parse(localStorage.getItem('x'));
    //storing back to model
    model.x.t = obj.t;
    model.x.d = obj.d; 
    // populating data on screen which was stored in local storage
    $scope.x.t = model.x.t ;
    $scope.x.d = model.x.d;
}

How to store data of yand z and should display on screen on switching to radio button?

如何存储y和z的数据,切换到单选按钮时应显示在屏幕上?

2 个解决方案

#1


5  

You should use JSON.stringify and JSON.parse. You have typo on your code and you don't need to set each property. Just set the x on scope.

您应该使用JSON.stringify和JSON.parse。你的代码上有拼写错误,你不需要设置每个属性。只需在范围上设置x即可。

var setInLocalStorage = function(){
    var x =  model.x
    localStorage.setItem('x', JSON.stringify(x));
}

var getFromLocalStorage = function() { 
    var obj = JSON.parse(localStorage.getItem('x'));
    model.x = obj;
    $scope.x = model.x;
}

But the best practice is not to do these type of actions in your controller. You should be using angular.factory or angular.service for this.

但最佳做法是不要在控制器中执行这些类型的操作。您应该使用angular.factory或angular.service。

In your x-storage.js

在你的x-storage.js中

angular.factory('xStorage', function(){
 var x = localStorage.getItem('x') || {};
 return {
  getX: function(){
    return x;
  },
  setX: function(xData){
    x = xData;
    localStorage.setItem('x', xData);
  }
 }
})

In your controller.js

在你的controller.js中

angular.controller('controllerName', function(xStorage){
 $scope.x = xStorage.getX();

 $scope.clickButton = function(anyValue) {
   xStorage.setX(anyValue);
 }
})

#2


1  

You can use setObject():

您可以使用setObject():

import { Component, OnInit } from '@angular/core';

import { CoolLocalStorage } from 'angular2-cool-storage';

@Component({
  selector: 'my-app'
})
export class AppComponent implements OnInit { 
    localStorage: CoolLocalStorage;

    constructor(localStorage: CoolLocalStorage) {
        this.localStorage = localStorage;   
    }

    ngOnInit() {
        this.localStorage.setItem('itemKey', 'itemValue');

        console.log(this.localStorage.getItem('itemKey'));

        this.localStorage.setObject('itemKey', {
            someObject: 3
        });

        console.log(this.localStorage.getObject('itemKey'));
    }
}

For More details :

更多细节 :

https://www.npmjs.com/package/angular2-cool-storage

https://www.npmjs.com/package/angular2-cool-storage

#1


5  

You should use JSON.stringify and JSON.parse. You have typo on your code and you don't need to set each property. Just set the x on scope.

您应该使用JSON.stringify和JSON.parse。你的代码上有拼写错误,你不需要设置每个属性。只需在范围上设置x即可。

var setInLocalStorage = function(){
    var x =  model.x
    localStorage.setItem('x', JSON.stringify(x));
}

var getFromLocalStorage = function() { 
    var obj = JSON.parse(localStorage.getItem('x'));
    model.x = obj;
    $scope.x = model.x;
}

But the best practice is not to do these type of actions in your controller. You should be using angular.factory or angular.service for this.

但最佳做法是不要在控制器中执行这些类型的操作。您应该使用angular.factory或angular.service。

In your x-storage.js

在你的x-storage.js中

angular.factory('xStorage', function(){
 var x = localStorage.getItem('x') || {};
 return {
  getX: function(){
    return x;
  },
  setX: function(xData){
    x = xData;
    localStorage.setItem('x', xData);
  }
 }
})

In your controller.js

在你的controller.js中

angular.controller('controllerName', function(xStorage){
 $scope.x = xStorage.getX();

 $scope.clickButton = function(anyValue) {
   xStorage.setX(anyValue);
 }
})

#2


1  

You can use setObject():

您可以使用setObject():

import { Component, OnInit } from '@angular/core';

import { CoolLocalStorage } from 'angular2-cool-storage';

@Component({
  selector: 'my-app'
})
export class AppComponent implements OnInit { 
    localStorage: CoolLocalStorage;

    constructor(localStorage: CoolLocalStorage) {
        this.localStorage = localStorage;   
    }

    ngOnInit() {
        this.localStorage.setItem('itemKey', 'itemValue');

        console.log(this.localStorage.getItem('itemKey'));

        this.localStorage.setObject('itemKey', {
            someObject: 3
        });

        console.log(this.localStorage.getObject('itemKey'));
    }
}

For More details :

更多细节 :

https://www.npmjs.com/package/angular2-cool-storage

https://www.npmjs.com/package/angular2-cool-storage