I have the following on an Angular 7 application:
<ng-container *ngIf="(post$ | async) as post; else loader">
<div *ngIf="post; else empty">
Post content
</div>
</ng-container>
<ng-template #empty>
Post not found
</ng-template>
<ng-template #loader>
Loading post
</ng-template>
This works fine when post is defined ...
However when post
is undefined the "Post not Found" does not show ...
... I just see the Loading message which does not disappears.
What am I missing?
Update
post$ is defined as follows in the component:
Note: post$ is not an array. See envelope.result[0] in the code.
export class PostDetailComponent implements OnInit {
@Input() postId: number;
post$: Observable<PostDetailModel>;
constructor(private postService: PostService) { }
ngOnInit() {
this.post$ = this.getPost();
}
getPost(): Observable<PostDetailModel> {
return this.postService.getByPostId(this.postId).pipe(
map((envelope: Envelope<GetByPostIdResponse>) => envelope.result[0]),
map((response: GetByPostIdResponse) => {
return response == null ? null : {
id: response.id,
title: response.title
// other properties
};
}));
}