5

I'm planning to use ionic native storage to store some translation history, whenever there's a word being translated. The translation action (date, translate word) will be store in the ionic native storage, and when I open history page, a list of translation history will be shown.

Here's the most basic code I got from the ionic official website:

export class HomePage {
  DataArray: Array<string> = [];

  constructor(public navCtrl: NavController, private storage: Storage) {

  }
  // set a key/value
  setData(){
  this.storage.set('age', 'Max');
  }
  // Or to get a key/value pair
  getData(){
  this.storage.get('age').then((val) => {
    console.log('Your age is', val);
  });
}
}

1 Answer 1

11

use getItem and SetItem

export class HomePage {
  DataArray: Array<string> = [];

  constructor(public navCtrl: NavController, private storage: NativeStorage) {

  }
  // set a key/value
  setData(){
  this.storage.setItem('keyOfData', JSON.stringify(DataArray));
  }
  // Or to get a key/value pair
  getData(){
  this.storage.getItem('keyOfData').then((val) => {
    console.log('Your age is', JSON.parse(val));
  });
}
}

the refrence Ionic native storage

Sign up to request clarification or add additional context in comments.

6 Comments

Do u get what i meant? i want to get an array of Data, not a single data. Please read my question. :)
@RongZhao no you can use array , i update the answer so you can use the array to store and restore
Can i have more detail on JSON.stringify(DataArray). how the data is being write into array list
this DataArray can be your array that hold the data , when store the value to storage make it in a string format and when retrive it parse it back to json to became an array again so you can use it anywhere in the app.
sorry. i still dont get it. could u make a sample for DataArray code how array is being store.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.