0

I want to send the object returned to my service with serviceName.setObject(myObject: object) for example so I can call that object from a different component, and use it to display some of it's data. In other words, I want to get a Character Object when the user selects it in a NameComponent so I can call it my StatisticsComponent, where I display the statistics of the Character like so {{ myCharacter.name }} for example. I'm still new to Angular and I thought I'd find the answer easily with a bit of research, but it doesn't seem to be the case.

As of my latest attempt, I am able to get an Observable of my Object and send all the data I need to the console log with the method down below :

this.myService.getAllInfosById(this.getPersonnageIdByName(change))
   .subscribe(response => {
       console.log("All Details of Personnage", response);
   });

And I'm pretty sure the subscribe is what I need to update, but I'd need help from you guys on this one. Thanks in advance.

1 Answers1

1

Your question isn't pretty much clear but I think you might looking for Generics

Example

getAllInfosById<Type>(arg: Type): Observable<Type> {
  return of(arg);
}

Usage

this.myService.getAllInfosById<Personnage>(this.getPersonnageIdByName(change))
  .subscribe(response => {
  console.log("All Details of Personnage", response);
});

Example: Stackblitz

paranaaan
  • 1,626
  • 2
  • 9
  • 17