将提示输入值存储为对象

时间:2017-02-20 07:19:33

标签: angular ionic2

您好我有一个提示登录表单,当用户输入数据时,点击完成按钮点击用户输入的数据应存储为对象,我需要访问它并能够看到该对象到我的控制台

import { Component } from '@angular/core';
import { NavController, NavParams, AlertController } from 'ionic-angular';
@Component({
    selector: 'page-login',
    templateUrl: 'login.html'
})

export class LoginPage {
//is this right way to store values from input tag
     xuser = {
                    name: '',
                    email: '',
                    phone: '',
                    country: ''
        }



  constructor(public navCtrl: NavController,
                    public alertCtrl: AlertController,
    ) { }
    check(){
            console.log("hellow");
            let alert = this.alertCtrl.create({
                title: 'User data',
                inputs: [
                    {
                        name: 'name',
                        placeholder: 'Full Name',
                        type: 'text'
                    },
                    {
                        name: 'email',
                        placeholder: 'Email',
                        type: 'email'
                    },
                    {
                        name: 'phone',
                        placeholder: 'phone Number',
                        type: 'number'
                    },
                    {
                        name: 'country',
                        placeholder: 'Country',
                        type: 'text'
                    }
                ],

                buttons: [
                    {
                        text: 'Done',
                        handler: data => {

                           console.log("data given", this.xuser);

                        } 

                    }
                ] 
            });
            alert.present();
        }

//and i need that data to be stored to my local storage and i need to access it
 writeFile(){    
        var obj = this.xuser;
        this.storage.set(obj).then(() => {
            this.storage.get(obj).then(() => {
                console.log(obj.user.name);
            });
        });

所以当用户点击"检查"警报随表单打开,我需要将其存储到本地存储,然后再次访问

1 个答案:

答案 0 :(得分:0)

检查here。  您将在处理程序的data参数中获取警报输入值。

buttons数组中,

buttons: [{
           text: 'Done',
           handler: data => {
                    this.writeFile(data);//you can access as data.name,data.email etc.
                    console.log("data given", data);
                    } 
          }] 

您必须编辑您的写入功能以使用数据对象进行设置。