0

I am trying to figure out how does Angular consumes a getById service. I start from the very beginning, I have exposed a Rest Api service that returns a Boolean value and checks if an item is in my favorite List or not : it gets the id of an element as a parameter. In my Front end, I am using a baseApi service for the Basic Crud operations and a resolver for every request: So what I need to do is how to pass the id from my method to the resolver and how to define the link structure (URL).

Here is my code for a better understanding:

  • My actual getById in my baseService:

getById(link, id:number){
  this.header=this.createHeader();
  return this.http.get(this.apiurl+link+id,this.header).map(this.extractData);

}
* My actual incorrect transform method in resolver:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Item> {

  return this.service.getById(item/isFavorite/1');

 }
  • And the method in the typescript file that gets the item that I need to send its id to the resolver:

checkItem(item : Item){

  this.checkfav=this.route.snapshot.data.isfav;

/**
 * checkfav is a bool that should m rest api defined in resolver return
 */


}
C.Gh
  • 107
  • 2
  • 6
  • "resolvers" as you call them, are static data. If you want to share data between route, you will have to use a sharing service. –  Aug 03 '18 at 12:28
  • The "resolver" is also a service that collects Data before loading them into the component and they are called so I am not inventing them. Before the use of resolver, I was using a service to manipulate the data but afterwords, I had to deal with Problem of synchronisation ,that's why I am using a resolver. – C.Gh Aug 03 '18 at 12:43
  • My bad, I misread your code and the documentation ! –  Aug 03 '18 at 12:47
  • C pas grave :)) – C.Gh Aug 03 '18 at 12:49
  • But you should consider using a subscrpition to get your data, snapshots are made to be static (definition of a snapshot) –  Aug 03 '18 at 12:49

2 Answers2

0

You can get it from route.params['id']; directly instead of getting it from some other function, if you have the same id in the url that is currently activated.

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Item> {    
   const id = route.params['id'];
   return this.service.getById('item/isFavorite/' + id);    
}
Amit Chigadani
  • 28,482
  • 13
  • 80
  • 98
  • Thanks for the response but I have already tried to do so but it doesn't work ,it returns undefined for the id in the url.. – C.Gh Aug 03 '18 at 12:59
  • Why would that be undefined? How do you fetch id then? How does your url look like? – Amit Chigadani Aug 03 '18 at 13:03
  • probably because it doesn't get the id, my url should have this format: http://localhost:8080/item/isFavorite/item.id – C.Gh Aug 03 '18 at 13:18
  • Can you show your route config? I mean routing-module. – Amit Chigadani Aug 03 '18 at 13:47
  • Here it is the path: {path: 'Employee_Dashboard', component: UserComponent, canActivate: [UrlPermission], resolve: { workingHours: Working_hoursResolver, isfav:IsFavResolver}}, – C.Gh Aug 03 '18 at 13:55
0

I do not completly understand what you want to achieve?

If you need to get information (for example an id) out of the current route, so that you can provide that information in your resolver, so that it is able resolve the needed data, the way of @Ammit Chigadani would be (nearly) correct

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Item> {    
   const id = route.params['id'];
   return this.service.getById('item/isFavorite/', id);    // <= Be aware of the two parameters URL and id
}

If your problem is, that your getById function is not working as expected, that may be because you call the method without the explicit "id" parameter. You call it

return this.service.getById(item/isFavorite/1');

and i am pretty sure you want to call it

return this.service.getById(item/isFavorite/' , 1);

I expect that 'ifav' is the name you have defined for your resolver? See at Angular.io. In that example you could get the resolved data by

this.route.snapshot.data.team

because the resolver was defined on "team".

warm regards

JanRecker
  • 1,787
  • 13
  • 17
  • By Clicking on a button , I need to check if my item belongs to my favorites or not.As a result , I have exposed a Rest Api service that returns true if the item is among my favorites and false if not. So, In other words, I am getting the item I want to test in this function checkFav and have developed a getById function that takes a link(url) and the parameter(item Id that I want to check from the back end). Question is how to send the id and manipulate it as a parameter? I have tried with the static url and It worked returning the excepted result: – C.Gh Aug 03 '18 at 13:07