首页 文章

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

提问于
浏览
0

我想知道如何在同一个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

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

    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
    }
    

相关问题