使用restcript和angular get数据使用rest API。我是angular 2和typescript的新手。请帮助我在我的应用程序中实现这一点
感谢
答案 0 :(得分:1)
使用以下可能对您有用的代码
public vehicleinfo:any=[]
//query is your rest api url
getVehicleinfo(query){
if (this.vehicleinfo) {
// already loaded data
// console.log("already data loaded vehicleinfo") ;
return Promise.resolve(this.vehicleinfo);
}
// don't have the data yet
return new Promise(resolve => {
// We're using Angular HTTP provider to request the data,
// then on the response, it'll map the JSON data to a parsed JS object.
// Next, we process the data and resolve the promise with the new data.
this.http.get(query)
.map(res => res.json())
.subscribe(data => {
// we've got back the raw data, now generate the core schedule data
// and save the data for later reference
this.vehicleinfo = data;
resolve(this.vehicleinfo);
});
});
}