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.

4
  • 1
    what do you want to do with the response object? Commented Dec 15, 2022 at 2:06
  • I clarified my question, thank you Commented Dec 15, 2022 at 3:06
  • Can you show more of your code? Which component has the service call? Commented Dec 15, 2022 at 4:25
  • Does this answer your question? RxJs get value from observable Commented Dec 15, 2022 at 5:33

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.