How to pass data from one screen to another with Native Localstorage and Ionic 3?

Asked

Viewed 145 times

0

I would like to know how to pass data from one item to a form in another view, using Ionic 3’s Native Localstorage.

I created a preview as shown below:

import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';

/*
  Generated class for the MovimentosProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class MovimentosProvider {

  lista:any[];
  chave:string = "movimentos";

  constructor(private storage: Storage) {
    this.storage.ready().then(()=>{
      this.storage.get(this.chave).then((registos) =>{
        if(registos){
          this.lista = registos;
        }else{
          this.lista = [];
        }
      });      
    });    
  }

  listar(){
    return this.lista;
  }

  adicionar(registro:any){    
    this.storage.ready().then(()=>{
      this.lista.push(registro);
      this.storage.set(this.chave, this.lista); 
    }); 
  }

}

And I have a button editarMovimento but I want to pass what you captured from the listing to the form and edit it.

editarMovimento(movimento:IMovimento){    
    this.movimento = movimento;

  }

How can I do?

1 answer

0


I’m not sure I understand your question exactly.

Inside the button you pass as parameter the key you want to edit, the same one you used in Storage.set

storage.get(SUA CHAVE AQUI).then((val) => {
    console.log(val);
  });

Remembering that the return is always string, so if you have saved an object you will need to transform the string into an object.

  • Thanks @Renan Rafael, it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.