Async pipe with rxjs

走远了吗. 提交于 2021-02-11 01:11:50

问题


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:


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)
);


来源:https://stackoverflow.com/questions/59448263/async-pipe-with-rxjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!