I'm a first-time student of the Ionic platform, and obviously I have some trouble with a little piece of my first application. I want to have a simple counter in a button: when this button is clicked, the counter increments by 1. Then I save the value using the Storage and I retrieve the same value in another page with "storage.get".
This is the code:
constructor(public storage:Storage, public platform: Platform){
this.storage = storage;
}
ionViewDidLoad() {
console.log("I'm alive!");
this.counter = this.storage.get("Count").then((data)=>{
console.log(data);
});
}
public counter = 0;
count(){
this.counter+=1;
this.storage.set("Count",this.counter);
}
When I re-open the app, I try to use the stored value to continue to increment the counter, but it starts again from 0: how can I increment the variable of the counter using the stored value?
Thank for your support!
You are setting this.counter to the promise call.
Do this instead:
Also, once you declare an injection a public in the constructor, you don't need to set it again inside the constructor. It, automatically, will be available with the instance of the class. Basically, you don't need this.