I have written the service code like this to read from backend
getData(): any {
let headers = new Headers(
{
});
let options = new RequestOptions({ headers: headers });
return this.httpClient.get('https://link', options)
.map((res: Response) => res.json())
// .do(data => console.log('All data: ' + JSON.stringify(data)))
.catch((error: any) => Observable.throw(error.json().error || 'Server error'));
and the code to load data
ngOnInit() {
this.service.getData().subscribe(
(res) => {this.Data = res},
(error) => this.errorMessage = <any>error);
}
but here it's getting data after the view loads so it's giving error.
How to load the data before view loads??