*ngIf else if in template

后端 未结 8 781
甜味超标
甜味超标 2020-12-07 11:33

How would I have multiple cases in an *ngIf statement? I\'m used to Vue or Angular 1 with having an if, else if, and else

相关标签:
8条回答
  • 2020-12-07 12:19

    To avoid nesting and ngSwitch, there is also this possibility, which leverages the way logical operators work in Javascript:

    <ng-container *ngIf="foo === 1; then first; else (foo === 2 && second) || (foo === 3 && third)"></ng-container>
      <ng-template #first>First</ng-template>
      <ng-template #second>Second</ng-template>
      <ng-template #third>Third</ng-template>
    
    0 讨论(0)
  • 2020-12-07 12:19

    <ion-row *ngIf="cat === 1;else second"></ion-row>
    <ng-template #second>
        <ion-row *ngIf="cat === 2;else third"></ion-row>
    </ng-template>
    <ng-template #third>
    
    </ng-template>

    Angular is already using ng-template under the hood in many of the structural directives that we use all the time: ngIf, ngFor and ngSwitch.

    > What is ng-template in Angular

    https://www.angularjswiki.com/angular/what-is-ng-template-in-angular/

    0 讨论(0)
提交回复
热议问题