ritesh_bhat ritesh_bhat - 19 days ago 10
Javascript Question

using LocalStorage ionic 2

I have two text fields and have to take data from both of them and store it using LocalStorage.
So here is the code I've implemented but it's not working can you tell me to solve it.

in page1.html

<ion-input [(ngModel)]="hour" type="number" ></ion-input>

<ion-input [(ngModel)]="min" type="number" ></ion-input>

<button clear (click)="setTime(hours.value,min.value)" item-right >settime</button>


in pag1.ts

export class Page1 {

public hour;
public min;
public local:Storage;

constructor(public nav: NavController, translate:TranslateService) {}


setTime(hour, min){

if(hour<24 && hour>0 && min>0 && min<61){
this.local.set('hour',JSON.stringify(hour));
this.local.set('min',JSON.stringify(min));
}
else{
console.log("OUTOF LIMIT TIME EXCEPTION the values are "+hour+min);
}
}

}


on console log, it is showing
[Object Object]
at the end

and please also tell about get from localStorage.
Thanks in advance :-)

Answer

You need to import both Storage and LocalStorage, and you need to add this to your constructor:

this.local = new Storage(LocalStorage);

Docs with and example are here: http://ionicframework.com/docs/v2/api/platform/storage/LocalStorage/