ChatGPT解决这个技术问题 Extra ChatGPT

如何使用“*ngIf else”?

我正在使用 Angular,我想在此示例中使用 *ngIf else(自版本 4 起可用):

<div *ngIf="isValid">
  content here ...
</div>

<div *ngIf="!isValid">
 other content here...
</div>

如何使用 ngIf else 实现相同的行为?


P
Peter Mortensen

角度 4 和 5:

使用 else

<div *ngIf="isValid;else other_content">
    content here ...
</div>

<ng-template #other_content>other content here...</ng-template>

您还可以使用 then else

<div *ngIf="isValid;then content else other_content">here is ignored</div>
<ng-template #content>content here...</ng-template>
<ng-template #other_content>other content here...</ng-template>

或单独的 then

<div *ngIf="isValid;then content"></div>
<ng-template #content>content here...</ng-template>

演示:

Plunker

细节:

<ng-template>:是 Angular 自己的 <template> 标签实现,即 according to MDN

HTML