【问题标题】:Is it possible to use variable with toastController in Ionic or Angular?是否可以在 Ionic 或 Angular 中将变量与 toastController 一起使用?
【发布时间】:2019-07-04 19:53:28
【问题描述】:

我想用我在变量中的名字向用户显示敬酒消息。可能吗?如果有,怎么做?

我想做这样的事情:

this.toastCtrl.create({
  message: "Welcome user.firstname" ,
  duration: 3000
}).present();

user.firstname 包含我用户的名字。

PS:上面的代码不起作用,它显示消息“Welcome user.firstname”

【问题讨论】:

  • 为什么不干脆做"Welcome " + user.firstname
  • 你已经有了答案。 Toast 中显示的message 是一个字符串。只需连接你的字符串,你就完成了!

标签: angularjs ionic-framework ionic3 toast


【解决方案1】:

你可以给toast添加参数

async toastCtrl(msg) {
  const toast = await this.toastController.create({
    message: msg,
    duration: 300
    });
  toast.present();
}

用变量调用函数

this.toastCtrl('Welcome ' + user.firstname);

【讨论】:

  • 将它包装在一个函数中如何改进简单地执行"Welcome " + user.firstname
  • 因为如果需要,您可以使用 toastController 来处理不同的消息
【解决方案2】:

简单地做:

this.toastCtrl.create(
    { 
        message: "Welcome " + user.firstname, 
        duration: 3000 
    }
).present();

【讨论】:

  • 非常感谢。我真正的困难是如何连接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
相关资源
最近更新 更多