0

我正在使用 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 显示这些数据,问题是当我单击按钮时图表中没有显示任何内容,我认为当我单击时数据还没有准备好

组件.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
    }]
  };
  }

在将它们发送到图表之前,如何获取所有数据?

4

1 回答 1

0

订阅 observable 时,可以提供回调函数;在下面的例子中,我称之为CompleteGet. CompleteGet()只会在返回数据而不是错误的成功获取时调用。您可以在回调函数中放置您需要的任何后续逻辑。

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.
}
于 2016-08-31T10:46:28.240 回答