// html
<div *ngFor"let post of postsForUser$ | async">
{{ post.title }}
</div>
// component
postsForUser$ = this.entedUser$.pipe(
switchMap(userName => this.userService.getUserId(userName)),
switchMap(userId => this.userService.getPostsForUserId(userId))
)
// service
private _userSubject = new Subject<string>();
enteredUser$ = this._userSubject.asObservable();
userChanged(userName: string): void{
this.enteredUser.next(userName);
}
getUserId(userName: string): Observable<number>{
return this.http.get<User[]>(`${userUrl}?userName=^${userName}$`).pipe(
catchError(this.handleError),
map(users => (users.lenght === 0) ? 0 : users[0].id)
)
}
getPostsForUserId(userId: number): Observable<Post[]>{
return this.http.get<Post[]>(`${postsUresgatrl}?userId=^${userId}$`).pipe(
catchError(this.handleError)
)
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter