2017-02-20 54 views
0

嗨我有一个提示警报,其中有一个登录表单,当用户输入数据,并在完成按钮单击由用户输入的数据应存储为对象,我需要访问它并且能够看到该对象到我的控制台存储提示输入值作为对象

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

所以当以往用户点击“检查”警报,窗体打开,我需要将其保存至本地存储和访问再次

+0

什么问题? –

+0

你会得到错误,你在哪里调用writeFile? –

+0

我不能存储用户输入的值,如果我使用html页面,那么我将使用ngmodel,但在提示如何让用户给的价值@GünterZöchbauer –

回答

0

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

在你buttons阵列,

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

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

+0

我可以访问提示功能外的数据 –

+0

您可以将其传递或设置为一个类变量 –