Angular experts! I'm trying to understand async pipes in Angular, but I'm stuck in a basic scenario. I have two select elements in the UI, one containing posts and one containing related comments. I want to set a posting (the last one) as the initially selected one for the select element displaying posts, and I want to use the selected item to filter related comments in the second select. This is not working in my code, for which I have created a simplified version in Stackblitz:
https://stackblitz.com/edit/angular-p6ynuy
Can any of you explain to me what I'm doing wrong? This is the relevant code fragment and HTML:
ngOnInit() {
this.postList$ = this.getPostList();
// latestPost$ is not is use yet, but maybe it could be used to set the selected post?
this.latestPost$ = this.postList$
.pipe(
map(posts => posts[posts.length - 1])
);
this.selectedPost$ = combineLatest([
this.postList$,
this.postSelectedAction$
])
.pipe(
map(([posts, selectedPostId]) => posts.find(post => post.id === selectedPostId))
);
this.commentList$ = this.selectedPost$
.pipe(switchMap(
post => this.getCommentList(post)
));
}
<select [ngModel]="selectedPost$ | async" (change)="onSelected($event.target.value)">
<option *ngFor="let post of postList$ | async" [ngValue]="post">
{{post.id}} {{post.title}}
</option>
</select>
<select>
<option *ngFor="let comment of commentList$ | async" [ngValue]="comment">
{{comment.id}} {{comment.postId}} {{comment.name}}
</option>
</select>