2

I have a little problem in async pipe Here is my case , I need to run nested observables in async pipe in html because i use on push strategy and i dont want to use some workarounds or change detector reference . My problem is , when i run the code below only the first observable is called Should i add return statements? Or whats the problem ?

Ts code

this.http.getUsers(criteria)
.pipe(map(data=>{
data.users.map(user=>{
this.http.getUserData(user.id)
.pipe(map(res=>{user.data=res.data}))}}

Html code

<div *ngFor=let user of users$ | async> </div>

1 Answer 1

2

You want to do a switchMap and you need to assign an observable to the users$ property.

users$ = this.http.getUsers(criteria).pipe(
  switchMap(user => this.http.getUserData(user.id)),
  map(res => res.data)
);
Sign up to request clarification or add additional context in comments.

4 Comments

I replaced the map with the switch map and i did assign the useres to observable and still only the first observable is called
Switch map into forkjoin.
Okay i forgot a brace , it worked according to adrian code it has ran successfully, and both observables were called but the users werent loaded in html
Did you do anything with the user template variable to display it like <div *ngFor=let user of users$ | async>{{ user | json }}</div> ? Your template code just creates an empty div per user and doesn't use the user variable.

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.