如何在 ionic 2 警报中显示此错误?
示例错误消息:
{
"name": [
"The name field is required."
],
"email": [
"The email field is required."
],
"password": [
"The password field is required."
]
}
编辑:当然,您还需要导入警报控制器:
Import { AlertController } from 'ionic-angular';
我修改了 Ionic 2 文档中给出的示例
constructor(private alertCtrl: AlertController) {
}
funTest(){
let test={"name":['The name field is required']};
let alert = this.alertCtrl.create({
title: "Error",
message: test.name.toString(),
buttons: ["OK"]
});
alert.present();
}
顺便问一下,你为什么使用数组?如果您只使用:
{"name":"The name field is required"}
你甚至不需要 .toString((。
是的,您可以使用AlertController
,但似乎您正在验证输入。为此,最好使用可以轻松验证Forms
。看看这个,希望它能有所帮助:http://ionicframework.com/docs/v2/resources/forms/