2017-02-20 55 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

您可以將其傳遞或設置爲一個類變量 –