Angular 2 Date deserialization

后端 未结 4 648
执念已碎
执念已碎 2020-12-05 07:27

I have an Angular 2 application. A service is requests data from an api that returns the results like the following:

{
    \"data\":[
        {\"id\":1,\"ti         


        
4条回答
  •  盖世英雄少女心
    2020-12-05 07:50

    I would map the string field to a date one:

    getList() : Observable {
      return this._http.get(this._getListUrl).map(this.extractData);
    }
    
    private extractData(res: Response) {
      var data = res.json().data || [];
      data.forEach((d) => {
        d.timestamp = new Date(d.timestamp);
      });
      return data;
    }
    

提交回复
热议问题