I am just starting learning angular2.I have problem while calling service in angular2.The service calls successfully,but I have problem how can I handle data. In Angular1 we do like this:
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."
);
}
Here How can I handle data Here is My service:
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));
}
}