Help with LocalStorage and Storage with the ionic@beta8

hey ionic team. i need your help with LocalStorage and Storage in ionic 2. i have 3 pages,
-the first is a list of user, (UserPage)
-the second is this detail list of user and (UserDetailPage)
-the last is the favorite page where i can save the favorite use. (FavoritePage)

  • my code is looks like this for the first page :

    loadData(){
    // serviceData is from my provider
    this.serviceData.load(‘data/myData.json’).then(data=>{
    load.dismiss();
    this.reponses=data;
    });
    }
    // this function is for userDetailPage
    goDetail(reponse){
    this.nav.push(VeterinaireDetailPage,reponse)
    }

    • for the second page
      // i can get data with the navParams
      this.params=params;
      this.response=params.data;
      // function to add to userDetail to favoritePage
      /*
      here is my problem. how can i add detail for one user with click event on a button, to the FavoritePage and save data with the LocalStorage or Storage in ionic2
      */

Thanks and Sorry for my english. need your help please :slight_smile:

Create a function where you get the favorites from localStorage, add the new favorite and save back to localStorage.

It would look something like this:

addFavorite(newFavorite) {
 // This gets the favorites already stored in localStorage
  this.local.get('favorites').then( favorites => {
    this.favoriteList = JSON.parse(favorites);

    // After you get the favorites push the new favorite to the list
    this.favoriteList.push(newFavorite);

    // And then just save it back to localStorage
    this.local.set('favoriteList', this.favoriteList);
  });
}

Just remember, JS is async, so make sure the data is always ready before you push or set.

Cheers!