18910140161

如何在从未满足条件的情况下显示特定HTML(角度)-堆栈溢出

顺晟科技

2022-10-18 12:15:17

71

假设我有一个围绕NGIF语句

的NGFOR循环
<ng-container *ngFor="let item of items">
  <ng-container *ngIf="condition1">
    //display table1
  </ng-container>
</ng-container>

现在,假设我有第二个表(称为表2),如果表1没有显示,我就想显示它。

<ng-container *ngIf="condition1 was never met and table1 is not displayed">
  //display table2
<ng-container>
最好的方法是

什么?有没有一种方法可以使用Angular的数据绑定功能来做到这一点?或者,你能给我指出正确的方向吗?


顺晟科技:

您可以使用ng-template

ngif-else
<ng-container
  *ngIf="condition1; then table1; else table2">
</ng-container>

<ng-template #table1>
  <div>
    table1
  </div>
</ng-template>
<ng-template #table2>
  <div>
    table2
  </div>
</ng-template>
<ng-container *ngFor="let item of items">
  <ng-container *ngIf="condition1;else table2">
    //display table1
  </ng-container>
</ng-container>
<ng-template #table2> //display table2 </ng-template>

您可以使用标准的角度NGIF,否则像这样

<代码><;NG-容器*NGIF=";条件1;然后是表1;否则表2";>;<;/NG-容器>;<;NG-Template#表格1>;<;细分(>;)表1<;/DIV>;<;/NG-模板>;<;NG-Template#表格2>;<;细分(>;)表2<;/DIV>;<;/NG-模板>;

你可以在这篇文章

中了解更多。
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航