我想知道如何在相同的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
提前感谢:)
发布于 2018-08-06 05:12:36
您想要做的是调用subscribe内部的方法。因此,当调用此方法时,您将确保数据已填充:
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
}
https://stackoverflow.com/questions/51698225
复制相似问题