简体   繁体   English

警报控制器setmessage在ionic 4中不起作用

[英]alert controller setmessage not working in ionic 4

In alertController I want to call prompt.setMessage() if the input field is empty .But this is not valid function in ionic4 在alertController中,如果输入字段为空,我想调用hint.setMessage(),但这在ionic4中无效

code

 const prompt= await this.alertController.create({
      header: 'insert text',
      message: 'enter text',
      inputs: [
        {
          name: 'itemtext',
          placeholder: 'enter text'
        }
      ],
      buttons: [{
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('Confirm Cancel: blah');
          }
        }, {
          text: 'Ok',
          handler: async (data:any) => {
            if(data.itemtext==""){
              prompt.setMessage("text should not be empty");
              return false;
            }
            else{
            console.log("data.itemtext");
            }
          }
        }
      ]
    });
    await prompt.present();

and I don't want to close alert promt if text is empty please help. 如果文本为空,我也不想关闭警报提示,请帮忙。

You no longer have the method in Ionic 4, but you could still directly change the message property to achieve what you want: 您不再拥有Ionic 4中的方法,但仍可以直接更改message属性以实现所需的功能:

const prompt= await this.alertController.create({
      header: 'insert text',
      message: 'enter text',
      inputs: [
        {
          name: 'itemtext',
          placeholder: 'enter text'
        }
      ],
      buttons: [{
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('Confirm Cancel: blah');
          }
        }, {
          text: 'Ok',
          handler: (data:any) => {
            if(data.itemtext==""){
              prompt.message = "text should not be empty";
              return false;
            }
            else{
              console.log(data.itemtext);
            }
          }
        }
      ]
    });
    await prompt.present();

I also removed "async" from your method in the handler as you do not need it there. 我还从处理程序中的方法中删除了“异步”,因为您在那里不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM