【问题标题】:How to pass retrieved json data to a function?如何将检索到的 json 数据传递给函数?
【发布时间】:2017-05-22 17:00:11
【问题描述】:

我想将 item.translatedText 传递给 read 函数。

这里是前端页面。

  <ion-card *ngFor="let item of items" >
    <ion-card-content>
      {{item.translatedText}}
      <ion-row>
      </ion-row>
      <button ion-button clear small icon-left color="primary" (click)="read()">
  <ion-icon name="musical-notes"></ion-icon>

这里是后端页面。

async read() : Promise<any> {
  //Read the text from the model via TTS
  try {
    await this.tts.speak(this.translatedText);
  }
  catch (e) {
    console.log(e);
  }
}

【问题讨论】:

  • 您的前端组件/服务代码会发生什么变化?
  • 为什么不在read 函数中将item.translatedText 作为参数传递?

标签: json angular ionic2


【解决方案1】:

听起来您可以直接将该值传递给 read 函数。你可以这样做:

模板:

  <ion-card *ngFor="let item of items" >
    <ion-card-content>
      {{item.translatedText}}
      <ion-row>
      </ion-row>
      <button ion-button clear small icon-left color="primary" (click)="read(item.translatedText)">
  <ion-icon name="musical-notes"></ion-icon>

然后更新 read 方法以接受翻译后的文本作为参数:

async read(translatedText) : Promise<any> {
  //Read the text from the model via TTS
  try {
    await this.tts.speak(translatedText);
  }
  catch (e) {
    console.log(e);
  }
}

【讨论】:

  • 我很高兴它成功了。既然成功了,你能接受这个答案吗?
猜你喜欢
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
  • 2019-11-04
  • 1970-01-01
  • 2020-09-19
  • 1970-01-01
  • 2021-09-30
  • 1970-01-01
相关资源
最近更新 更多