如何在离子的ts文件中使用来自Api的数据

时间:2018-08-05 21:00:28

标签: html angular api typescript ionic-framework

我想知道如何在同一个ts文件中使用从API获取的数据。我可以使用此数据,并使用此代码将其显示在HTML中。

loadAppInfo() {

  this.http.get('Here goes my url')
  .map(res => res.json())
  .subscribe(data => {
    this.AppInfo = data.records;
    console.log(data.records);
  },err => {
    console.log(err);
  });
}
<ion-option *ngFor="let data of AppInfo">{{ data.AppName }}</ion-option>

使用此代码,我可以毫无问题地显示HTML数据。但是我想将此单一数据设置为ts文件中的变量,然后使用它。

有可能吗?

这是在控制台中打印AppInfo之后的数据外观。 Here is the Image

预先感谢:)

1 个答案:

答案 0 :(得分:0)

您想要做的是在订阅中调用一个方法。因此,当调用此方法时,您将确保数据已填充:

loadAppInfo() {

  this.http.get('Here goes my url')
  .map(res => res.json())
  .subscribe(data => {
    this.AppInfo = data.records;
    this.AppName = this.AppInfo.AppName; // add "public AppName;" declaration in your class
    console.log(data.records);
    this.computeWithFilledData();
  },err => {
    console.log(err);
  });
}

computeWithFilledData() {
    // use this.AppInfo here
}