角度* ngIf =“ data $异步为数据&& data.payload;否则为noContent”

时间:2019-12-20 23:30:46

标签: angular templates angular-ng-if angular-template angular-template-variable

我只想在有数据的情况下有条件地显示一个块,否则我将显示一个noContent块。 从服务器获取数据。

<ng-container *ngIf="data$ async as data && data.payload; else noContent">
{{data.payload | json}}
</ng-container>

<ng-template #noContent> No content found </ng-template>

注释: 1-我不想实现两个s,即:

<ng-container *ngIf="data$ async as data ; else noContent">
  <ng-container *ngIf="data.payload; else noContent">
  {{data.payload | json }}
  </ng-container>
</ng-container>
...

2-以下语句有效

*ngIf="xx && yy"
*ngIf="data$ as data"
*ngIf="xx && data$ as data"
*ngIf="(data$ as data)?.payload?.length>0; else noContent"

1 个答案:

答案 0 :(得分:0)

尝试这样

<ng-container *ngIf="(data$ | async ) as d; else noContent">
    <ng-container *ngIf="d.payload?.length > 5 ;else noContent">
        <h3>
            {{d.payload | json}}
        </h3>
    </ng-container>
</ng-container>
<ng-template #noContent> No content found </ng-template>

demo ??