angular5 - *ngIf 中的 AsyncPipe

标签 angular5 rxjs5

我试图根据 *ngIf 中可观察到的 isComplex$ 值来显示 header 之一:

<complex-header *ngIf="isComplex$ | async; else ordinaryHeader"></complexheader>
<ng-template #ordinaryHeader>
  <ordinary-header></ordinary-header>
</ng-template>

问题是 *ngIf 与 else 情况一起使用,而不等待 observable 发出值。如何完成两个 header 等待 observable 发出其第一个值。

最佳答案

使用 angular5 *ngIf else 指令。

<div class="course-detail" *ngIf="isComplex$ | async as isComplex else ordinary">
   <complex-header *ngIf="isComplex"></complex-header>
   <ordinary-header *ngIf="!isComplex"></ordinary-header>
</div>
<!-- Showing ordinary header for cases when observable is not returned this can be replaced wuth loader as well-->
<ng-template #ordinary>
    <ordinary-header></ordinary-header>
</ng-template> 

另请参阅https://blog.angular-university.io/angular-reactive-templates/

关于angular5 - *ngIf 中的 AsyncPipe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50589564/

相关文章:

jquery - Angular 5 - 如何在使用 *ngFor 创建 HTML 元素时调用函数

javascript - 个人资料图片未跨组件更新 Angular 5

Angular 2 RxJS 可观察到的 : RetryWhen filter retry on error Status

angular - 在 RxJs 5 中共享 Angular Http 网络调用结果的正确方法是什么?

javascript - 使用ngx-print打印图像的问题

javascript - Angular 6 正确初始化自定义输入组件

angular - RxJS 5 与 Angular 2 : Retry failed Observable but then forward error

ajax - 使用 Rxjs 并发 Ajax 请求

javascript - RXJS - 仅在空闲时启动计时器?

events - Angular 5 ngSubmit @Output 触发两次