获取用户输入值,将其存储到本地存储 [英] get user input value store it to localstorage

查看:89
本文介绍了获取用户输入值,将其存储到本地存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取用户输入值,将其存储为对象,并在以后需要时使用它

I am trying to get user input value, store it as an object and use it later whenever it is required

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

 @Component({
     selector: 'page-login',
     templateUrl: 'login.html'
 })
 export class LoginPage {
     file;
     user = {
         Name: '',
         email: '',
         phone: '',
         Country: ''        
     };
     constructor(public storage: Storage) { }

     writeFile(){    
         var obj = this.user;
         this.storage.set(obj).then(() => {
             this.storage.get(obj).then(() => {
                 console.log(obj.user);
             });
         });
     }
 }

我的html是

<ion-content  padding>
    <form  #form="ngForm" (ngSubmit)="logForm(form)" novalidate>
        <ion-list inset  position="bottom">
            <ion-item>
                <ion-label>Name</ion-label>
                <ion-input [(ngModel)]="user.Name" name="Name" required type="text"></ion-input>
            </ion-item>

            <ion-item>
                <ion-label >Email</ion-label>
                <ion-input [(ngModel)]="user.email" name="email" required type="Email"></ion-input>
            </ion-item>
             <ion-item>
                <ion-label >Phone Number</ion-label>
                <ion-input [(ngModel)]="user.phone" name="phone" required type="number"></ion-input>
            </ion-item>
             <ion-item>
                <ion-label >Country</ion-label>
                <ion-input [(ngModel)]="user.Country" name="Country" required type="string"></ion-input>
            </ion-item>
        </ion-list>   
            <button ion-button block (click)="writeFile()">write file</button>   
    </form> 
</ion-content>

因此,当用户输入值时,它将被传递到listing.ts文件中的用户".我需要将其存储到本地存储中.我需要它在控制台中看到该对象.

So, when the user enters the values, it will be passed to "user" in listing.ts file. I need to store it to a local storage. I need it see that object in my console.

我尝试过,但出现错误:

I tried and I got error:

推荐答案

我认为,当您执行this.storage.set(obj);时,这是一个承诺.当您尝试在this.storage.get(obj).then(() => { console.log(obj.user); })上查看它时,仍无法解决.

I think, when you do this.storage.set(obj);, this is a promise. It is not resolved yet when you try to view it at this.storage.get(obj).then(() => { console.log(obj.user); }).

使您的代码如下:

writeFile(){    
    var obj = this.user;
    this.storage.set(key,value).then(() => {
        this.storage.get(obj).then(() => {
            console.log(obj.user);
        });
    });
}

这应该有效.让我知道它是否有效. :)

This should work. Let me know if it works. :)

编辑:另外,您还必须将其设置为键值格式,如

EDIT : Also, you have to set it in key value format as shown like this.

我还有另一种进行本地存储的机制. 就像这样:

I also have a different mechanism by which I do local storage. It is like this:

localStorage.setItem('user',JSON.stringify(jsonObject));
var temp = JSON.parse(localStorage.getItem('user'));
console.log("Temp - ",temp);

这也很好.

这篇关于获取用户输入值,将其存储到本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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