首页 > 解决方案 > 调用服务后如何调用不同的 Ionic toast?

问题描述

出于某种原因,在角度控制器中的服务响应之后,我遇到了调用离子吐司的问题。

基于响应的精确字符串,我必须初始化不同的消息。我已经检查了服务的正确响应(按我的预期工作)。

这是我所做的:

showToast(position: string, message: string) {
  let toast = this.toastCtrl.create({
    message: message,
    duration: 2000,
    position: position
  });
  toast.present(toast);
}

logForm() {
  console.log(this.userRegister.value);
  this.utente = this.userRegister.value;
  this.UtentiService.addUser(this.utente);

  //controllo se l'utente esiste già
  this.UtentiService.getSingleUser(this.utente).subscribe(
    data => {
      this.risposta = JSON.stringify(data.messaggio);
    },
    error => {
      console.log(error);
    },
    () => {
      alert(this.risposta);
      if (this.risposta === "ko_singoloFound") {
        this.showToast('bottom', 'Ti sei registrato con successo!');
      }
      if (this.risposta === "ok_singoloFound") {
        this.showToast('middle', 'Utente già registrato!');
      }
    });
}

显然,该函数似乎不会出现在最后一个回调中的 if 语句中。

我做错了什么?谢谢你

标签: angularionic3android-toast

解决方案


显然问题出在 JSON.stringify(data.messaggio); 答案已经是一个字符串并返回给我 ""ko_singoloFound"" ko_singoloFound。


推荐阅读