Rong Zhao
Rong Zhao

Reputation: 99

How to pass retrieved json data to a function?

I want to pass the item.translatedText to the read function.

Here's front end page.

  <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>

Here's back end page.

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

Upvotes: 1

Views: 60

Answers (1)

rmc00
rmc00

Reputation: 887

It sounds like you can simply pass that value into the read function directly. Here's how you could do that:

Template:

  <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>

And then an update to the read method to accept the translated text as a parameter:

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

Upvotes: 2

Related Questions