Angular 将提示输入值存储为对象
您好,我有一个带有登录表单的提示警报,当用户向其输入数据并单击“完成”按钮时,用户输入的数据应存储为对象,我需要访问它并能够在我的控制台上看到该对象Angular 将提示输入值存储为对象,angular,ionic2,Angular,Ionic2,您好,我有一个带有登录表单的提示警报,当用户向其输入数据并单击“完成”按钮时,用户输入的数据应存储为对象,我需要访问它并能够在我的控制台上看到该对象 import { Component } from '@angular/core'; import { NavController, NavParams, AlertController } from 'ionic-angular'; @Component({ selector: 'page-login', templateUrl:
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);
});
});
所以,每当用户单击“检查”时,就会打开带有表单的警报,我需要将其存储到本地存储,然后再次访问它。
您将在处理程序的数据参数中获得警报输入值
在按钮
数组中
buttons: [{
text: 'Done',
handler: data => {
this.writeFile(data);//you can access as data.name,data.email etc.
console.log("data given", data);
}
}]
您必须编辑写入函数以使用数据对象进行设置。有什么问题吗?是否出现错误?您在哪里调用writeFile?我无法存储用户输入的值,如果我使用html页面,那么我将使用ngmodel,但在提示中如何获取用户给定值@Günter Zöchbauer有什么区别?你是说访问用户输入的值?并发送到您的函数?我可以在提示函数之外访问此数据吗?您可以将其传递或设置为类变量