我剛剛開始學習angular2.I在調用angular2服務時出現問題。服務調用成功,但是我遇到問題,我該如何處理數據。在Angular1
,我們這樣做:Angular2中的調用服務
DemoService.getExchangeDataConnection().then(function(response){
//here we handle all the data
})
Angular2
constructor(private _sampleService: SampleService){
this._sampleService = _sampleService;
}
onClickMe(){
this.clickMessage ='You are my hero!';
this.error = "";
this.countries = [];
this._sampleService.test()
.subscribe(
data => this.countries = data,
error => this.error = "Region " + this.region + " is invalid."
);
}
在這裏,我該如何處理數據 這裏是我的服務:
export class SampleService {
constructor(http: Http){
this.http = http;
}
test(){
console.log(AppSettings.API_ENDPOINT);
return this.http.get(AppSettings.API_ENDPOINT+"oceania").toPromise()
.then(res => console.log(res.json()), err => console.log(err));
}
}
不要轉換您HTTP調用承諾。 – Chandermani