サービスリクエストからデータを受信しましたが、「差異」属性を追加します。これは私がURLから受け取るものです:データサービスリクエストを編集するAngular2
{
"times" : [{
"pta" : "12:05",
"ata" : "12:34"
},{
"pta" : "14:40",
"ata" : "14:36"
},{
"pta" : "12:05",
"ata" : "12:10"
},{
"pta" : "18:30",
"ata" : "20:00"
}]
}
これは私のサービスです。
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class TimesService {
constructor(private http: Http) { }
private _timesUrl = './app/api/times.php';
calculateDifference(data: any): any {
// Code to calculate difference here
// Return object with pta, ata, difference properties
}
getTimes(): Observable<any[]> {
return this.http.get(this._timesUrl)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
if(res.status < 200 || res.status >= 300) {
throw new Error('Bad respons status: ' + res.status);
}
let body = res.json();
return body.data || {};
}
private handleError(error: any) {
// In a real world app, we might send the error to remote logging infrastructure
let errMsg = error.message || 'Server error';
console.error(errMsg); // Log to console instead
return Observable.throw(errMsg);
}
}
私はそれが返される前に、すべてのtimesetにプロパティdifference
を追加するcalculateDifference()関数を呼び出すことができますどのように?達成しようとしていることを実装するためのより良い方法はありますか?