ActivatedRoute does not call the service before updating the angular page 5

0

Hello, I used the RouterLink which passes an id to the url, I look for this id and I step as a parameter to the function that searches the api data, but when I click on my link it redirects to the route I want, but not shows my data I'm iterating, and the api return array gets 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}');
}
    
asked by anonymous 10.05.2018 / 19:57

1 answer

0

The value will only exist within sbuscribe because it has the return of http 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 the non-angled pipe async

    
10.05.2018 / 22:07