等到http get完成,得到returns个数据
Wait until http get is completed and returns data
我正在使用 http 从服务器获取数据,在 service.ts
:
getAllCursenValue():Observable<Cursen[]>{
let cursen$ = this.http
.get(this.baseUrl,{headers: this.getHeaders()})
.map(mapCursen);
return cursen$;
}
function mapCursen(response:Response):Cursen[]{
return response.json().map(toCursen);
}
function toCursen(r:any):Cursen{
let cursen = <Cursen>({
id_boitier:r.id_boitier,
id_phase:r.id_phase,
date:new Date(r.date),
power:r.power,
voltage:r.voltage,
current:r.current,
phase:r.phase,
thd:r.thd,
acc:r.acc,
flags:r.flags,
added_info:r.added_info
});
return cursen;
}
在我的组件中,我想使用 highchart 显示此数据,问题是当我点击按钮时图表中没有显示任何内容,我认为当我点击时数据还没有准备好
component.ts :
getCursenByDateTest(){
this.cursenService
.getCursenValueByDateTest("2016-7-30","2016-7-31")
.subscribe(p => {
this.cursens = p;
console.log(p)
console.log(this.cursens.length);
});
}
单击按钮时的方法:控制台在打印对象之前打印 "GGGGG" 和 "PPPPP":
ChartLine(){
this.getCursenByDateTest();
console.log("GGGGGGGGGGGGGG");
console.log("PPPPPPPPPPPPPPPPPP",this.finaldate1);
this.options1 = {
chart: {
type: 'area'
},
title: {
text: 'Area chart with negative values'
},
xAxis: {
categories: this.voltagesMax
},
credits: {
enabled: false
},
series: [{
name: 'John',
data: this.voltagesMax
}]
};
}
我怎样才能所有数据在发送到图表之前?
当你订阅一个observable时,你可以提供一个回调函数;在下面的例子中,我称之为 CompleteGet
。 CompleteGet()
只会在成功获取 returns 数据而不是错误时调用。您可以在回调函数中放置您需要的任何后续逻辑。
getCursenByDateTest(){
this.cursenService
.getCursenValueByDateTest("2016-7-30","2016-7-31")
.subscribe(p => {
this.cursens = p;
console.log(p)
console.log(this.cursens.length);
},
error => this.error = error,
() => this.CompleteGet());
}
completeGet() {
// the rest of your logic here - only executes on obtaining result.
}
我正在使用 http 从服务器获取数据,在 service.ts
:
getAllCursenValue():Observable<Cursen[]>{
let cursen$ = this.http
.get(this.baseUrl,{headers: this.getHeaders()})
.map(mapCursen);
return cursen$;
}
function mapCursen(response:Response):Cursen[]{
return response.json().map(toCursen);
}
function toCursen(r:any):Cursen{
let cursen = <Cursen>({
id_boitier:r.id_boitier,
id_phase:r.id_phase,
date:new Date(r.date),
power:r.power,
voltage:r.voltage,
current:r.current,
phase:r.phase,
thd:r.thd,
acc:r.acc,
flags:r.flags,
added_info:r.added_info
});
return cursen;
}
在我的组件中,我想使用 highchart 显示此数据,问题是当我点击按钮时图表中没有显示任何内容,我认为当我点击时数据还没有准备好
component.ts :
getCursenByDateTest(){
this.cursenService
.getCursenValueByDateTest("2016-7-30","2016-7-31")
.subscribe(p => {
this.cursens = p;
console.log(p)
console.log(this.cursens.length);
});
}
单击按钮时的方法:控制台在打印对象之前打印 "GGGGG" 和 "PPPPP":
ChartLine(){
this.getCursenByDateTest();
console.log("GGGGGGGGGGGGGG");
console.log("PPPPPPPPPPPPPPPPPP",this.finaldate1);
this.options1 = {
chart: {
type: 'area'
},
title: {
text: 'Area chart with negative values'
},
xAxis: {
categories: this.voltagesMax
},
credits: {
enabled: false
},
series: [{
name: 'John',
data: this.voltagesMax
}]
};
}
我怎样才能所有数据在发送到图表之前?
当你订阅一个observable时,你可以提供一个回调函数;在下面的例子中,我称之为 CompleteGet
。 CompleteGet()
只会在成功获取 returns 数据而不是错误时调用。您可以在回调函数中放置您需要的任何后续逻辑。
getCursenByDateTest(){
this.cursenService
.getCursenValueByDateTest("2016-7-30","2016-7-31")
.subscribe(p => {
this.cursens = p;
console.log(p)
console.log(this.cursens.length);
},
error => this.error = error,
() => this.CompleteGet());
}
completeGet() {
// the rest of your logic here - only executes on obtaining result.
}