Html 如何在ionic中使用ts文件中的Api数据
我想知道如何在同一个ts文件中使用从API获取的数据。我能够使用此代码使用此数据并在HTML中显示它Html 如何在ionic中使用ts文件中的Api数据,html,angular,api,typescript,ionic-framework,Html,Angular,Api,Typescript,Ionic Framework,我想知道如何在同一个ts文件中使用从API获取的数据。我能够使用此代码使用此数据并在HTML中显示它 loadAppInfo(){ this.http.get('我的url在这里') .map(res=>res.json()) .订阅(数据=>{ this.AppInfo=data.records; 控制台日志(数据记录); },err=>{ 控制台日志(err); }); } {{data.AppName}您要做的是在subscribe中调用一个方法。因此,当调用此方法时,您将确保数据已填充
loadAppInfo(){
this.http.get('我的url在这里')
.map(res=>res.json())
.订阅(数据=>{
this.AppInfo=data.records;
控制台日志(数据记录);
},err=>{
控制台日志(err);
});
}
{{data.AppName}
您要做的是在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
}
嗯,你不这样做吗,AppInfo不是你ts文件中的一个变量吗?是的,我的文件中有AppInfo作为一个变量,但我在执行console.logYes时未定义,这个方法将被调用,但当我运行“console.log(this.AppInfo);”时,我将“undefined”作为输出。你在哪里运行这个console.log?如果在computeWithFilledData()中运行它,它不应该是未定义的…是的,您是对的,当我在方法中运行它时,它不是未定义的,但是我如何从中访问单个值?您能更明确一些吗?您所说的“从中访问单个值”是什么意思?我的意思是AppInfo是一个对象,它有其他值,如AboutText、AboutTitle、AppName等。例如,如何在AppInfo中获取值AppName?