角度[ngIf],[ngIfElse]和[ngIfElseIf]

问题描述

我现在有以下代码

<ng-container *ngIf="someCondition">
    <ng-template [ngIf]="cd.typedType === 'first'" [ngIfElse]="Second">
      <div class="row">
      first
      </div>
    </ng-template>
    <ng-template #Second>
      <div class="row">
      second
      </div>
    </ng-template>
</ng-container>

它正在工作。 如果有其他方法可以使用吗?要创建类似的内容

<ng-container *ngIf="someCondition">
    <ng-template [ngIf]="cd.typedType === 'first'" [ngIfElseIf]="cd.typedType === 'Second'" [ngIfElse]="cd.typedType === 'Third'>
      <div class="row">
      first
      </div>
    </ng-template>
    <ng-template #Second>
      <div class="row">
      second
      </div>
    </ng-template>
    <ng-template #Third>
      <div class="row">
      third
      </div>
    </ng-template>
</ng-container>

那么要使用基本的if / elseif / else在3个不同的模板之间移动吗?

解决方法

ngSwitch似乎是您正在寻找的答案!

data

在这里获取更多信息: angular.io/api/common/NgSwitchCase