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??
Best option is to use a Routeresolver. Resolvers are part of the route definition and intended to provide data before a route is activated. So if the view is shown you can get the provided data in the ngOnInit. You just have to use your code to fetch data in the resolver
In the Component: export class BesuchdetailComponent {
Add Routerdefinition: