使用LocalStorage ionic 2 [英] using LocalStorage ionic 2

查看:65
本文介绍了使用LocalStorage ionic 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本字段,必须从两个文本字段中获取数据并使用LocalStorage进行存储. 所以这是我已经实现的代码,但是无法正常工作,您能告诉我解决它吗.

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.

在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>

在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);
   }
 }

  }

在控制台日志上,它显示 [Object Object]最后

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

,并且还请告诉您有关从本地存储中获取的信息. 在此先感谢:-)

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

推荐答案

您需要同时导入StorageLocalStorage,并将其添加到构造函数中:

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

this.local = new Storage(LocalStorage);

带有和示例的文档在这里: http://ionicframework.com/docs/storage/

Docs with and example are here: http://ionicframework.com/docs/storage/

这篇关于使用LocalStorage ionic 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆