0

I would like to return a child element of an observable as this.

Say I have a HttpClient that gets an observable of people:

export class People{

    public Name: string;
}

and a service that gets a single one as abservable:

public get(id: number): Observable<People> {
    return this.http.get<People>('http://localhost:8801/' + "people/" + id);
  }

Now I would like to get the name from my observable as observable:

public get(id: number): Observable<string>{
    this.peopleService.get(id) .... ?
}

How could this be done?

2 Answers 2

1

You can use map() function to transform people observable:

const nameObservable = this.peopleService.get(id).pipe(
    map(people => people.name)
);
Sign up to request clarification or add additional context in comments.

Comments

0

subscribe to the event

  this.peopleService.get(id).subscribe((item) => {
   console.log(item.name)
 })

1 Comment

No I don't want to get the name I want to return the name as a Observable<string>. In specific I would like to bind async to the name property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.