【问题标题】:Keep the the alert box without dismiss if there is an error如果出现错误,请保留警告框而不关闭
【发布时间】:2019-05-14 14:07:41
【问题描述】:

这只是一个带有单选按钮的警报框。除了一件事,一切都很好。也就是说,如果出现错误,我无法保留警报框,直到用户输入正确的数据。根据the doc,我使用了return false 功能。但还没有运气。有什么线索吗?

Note: 如果输入为text boxes,则可以正常工作。这里我需要单选按钮。

const allApiKeys = await this.apiKeySqliteProvider.getAllApiKeys();
            const alert = this.alertCtrl.create();
            alert.setTitle('Select Api Key');
            forEach(allApiKeys, (apiKey: ApiKey) => {
              alert.addInput({
                type: 'radio',
                label: apiKey.name,
                value: apiKey.key,
                checked: false
              });
            });
            alert.addButton({
              text: 'Cancel',
              role: 'cancel',
              handler: data => {
                this.loading.dismissLoader(loading);
              }
            });
            alert.addButton({
              text: 'OK',
              handler: data => {
                let navTransition = alert.dismiss();
                navTransition.then(() => {
                 if (data == null) {
                    this.showToastProvider.showErrorToast("Invalid API Key");
                    this.loading.dismissLoader(loading);
                    return false;
                  } 
                });
                return false;
              }
            });
            alert.present();
          } 

【问题讨论】:

  • "return false" 不是实际问题。您正在调用 alert.dismiss,这就是问题所在。如果您不想隐藏警报,则应将关闭代码移至 else 块中。

标签: angular typescript ionic-framework ionic3


【解决方案1】:

return false 不是实际问题。你打电话给alert.dismiss,这就是问题所在。如果您不想隐藏警报,则应将 dismiss 代码移至 else 块内。

请将您的代码更改为以下

 const allApiKeys = await this.apiKeySqliteProvider.getAllApiKeys();
                const alert = this.alertCtrl.create();
                alert.setTitle('Select Api Key');
                forEach(allApiKeys, (apiKey: ApiKey) => {
                  alert.addInput({
                    type: 'radio',
                    label: apiKey.name,
                    value: apiKey.key,
                    checked: false
                  });
                });
                alert.addButton({
                  text: 'Cancel',
                  role: 'cancel',
                  handler: data => {
                    this.loading.dismissLoader(loading);
                  }
                });
                alert.addButton({
                  text: 'OK',
                  handler: data => {
                      if (data == null) {
                        this.showToastProvider.showErrorToast("Invalid API Key");
                        this.loading.dismissLoader(loading);
                        return false;
                      }

                      // you don't need else here as by default it will hide alert
                  }
                });
                alert.present();
              } 

【讨论】:

  • 这是我犯的一个非常基本的错误。非常感谢您向我展示它。
  • 任何时候 :) 很高兴我帮助了 :)
猜你喜欢
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 2021-10-18
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
相关资源
最近更新 更多