Activatedroute does not call the service before updating angular page 5

Asked

Viewed 121 times

0

Hello, I used the routerLink that passes an id to the url, which looks for this id and step as parameter for the function that searches the data of the api, but when I click on my link it redirects to the route I want, but it does not show my data that I am iterating, and the api return array is Undefined. But if I reload the route again then yes it shows the data:

This is my link

<a routerLink="/wines/{{uvas.id_uva}}">{{uvas.uva}}</a></li>

My Component

ngOnInit() {

this.Wine = this.route.paramMap.pipe(
switchMap((params: ParamMap) =>
  this._dataService.getWine(params.get('id'))

  );
}

My Service

getWine(id): Observable<IWine> {
return this.http.get<IWine>(`http://localhost:8888/exemplo/${id}`);
}
  • I will comment on the problem I had, all the answers that were posted here were right and working, the problem was in the angular2-materialize Carousel that I am using, the data is being iterated in this Carousel after it was already loaded, and then not loading the data that came from the service...

1 answer

0


The value will only exist within sbuscribe because it has the http return and equivalent to Promise.then..

ngOnInit() {
 this.activatedRoute.params.map(params => params['id'])
.switchMap(id => id ? this.dataService.getWine(id) : Observable.empty())
.subscribe(wines=>   this.Wines = wines);

}

If you have more questions search for asyncornicity. More specifically async pipe at the angle

  • Poise, already changed using async pipe and nothing, the problem I call the route and it does not show the data, but when reload again from there it shows

  • I edited my answer, basically you were taking the snapshot instead of seeing the observable of params.

  • tried but still nothing, I will post another attempt I did, it worked it looks for every information I need according to the id of the route I clicked, but still need to reload the route to show the data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.