*ng if if if in template

80 templates angular-ng-if angular

我如何在*ngIf声明中有多个案例?我习惯于Vue或Angular 1有一个if,else ifelse,但似乎Angular 4只有一个true(if)和false(else)条件.

根据文档,我只能这样做:

  <ng-container *ngIf="foo === 1; then first else second"></ng-container>
  <ng-template #first>First</ng-template>
  <ng-template #second>Second</ng-template>
  <ng-template #third>Third</ng-template>
Run Code Online (Sandbox Code Playgroud)

但我希望有多种条件(类似):

  <ng-container *ngIf="foo === 1; then first; foo === 2; then second else third"></ng-container>
  <ng-template #first>First</ng-template>
  <ng-template #second>Second</ng-template>
  <ng-template #third>Third</ng-template>
Run Code Online (Sandbox Code Playgroud)

但我最终不得不使用ngSwitch,感觉就像一个黑客:

  <ng-container [ngSwitch]="true">
    <div *ngSwitchCase="foo === 1">First</div>
    <div *ngSwitchCase="bar === 2">Second</div>
    <div *ngSwitchDefault>Third</div>
  </ng-container>
Run Code Online (Sandbox Code Playgroud)

或者,似乎很多Angular 1和Vue中习惯使用的语法在Angular 4中都不受支持,那么建议用这样的条件构造代码的方法是什么?

Cor*_*elC 104

另一种选择是筑巢条件

<ng-container *ngIf="foo === 1;else second"></ng-container>
<ng-template #second>
    <ng-container *ngIf="foo === 2;else third"></ng-container>
</ng-template>
<ng-template #third></ng-template>
Run Code Online (Sandbox Code Playgroud)

  • 这对我来说是更好的解决方案.我的条件基于多个变量,并且不止一个变量能够同时成立. (4认同)
  • 我们不能使用 `&lt;ng-template #second *ngIf="foo === 2;elsethird"&gt;` (2认同)
  • 链接 ngIf 时,由前一个 else 寻址的 &lt;ng-template&gt; 不支持另一个 ngIf。嵌套的 &lt;ng-container&gt; 标签允许插入下一个条件。 (2认同)

小智 31

你可以使用:

<ng-template [ngIf]="index == 1">First</ng-template>
<ng-template [ngIf]="index == 2">Second</ng-template>
<ng-template [ngIf]="index == 3">Third</ng-template>
Run Code Online (Sandbox Code Playgroud)

除非ng-container部件对你的设计很重要,我想.

这是一个Plunker

  • 我的例子有点简单,但期待'else if'行为,这样`if(index === 1)else if(foo === 2)`必须写成`if(index === 1 ) if (index !== 1 &amp;&amp; foo === 2)` 这有点乱,更容易出错,我们必须写更多的逆逻辑。 (7认同)

goa*_*oat 21

这似乎是最干净的方法

if (foo === 1) {

} else if (bar === 99) {

} else if (foo === 2) {

} else {

}
Run Code Online (Sandbox Code Playgroud)

在模板中:

<ng-container *ngIf="foo === 1; else elseif1">foo === 1</ng-container>
<ng-template #elseif1>
    <ng-container *ngIf="bar === 99; else elseif2">bar === 99</ng-container>
</ng-template>
<ng-template #elseif2>
    <ng-container *ngIf="foo === 2; else else1">foo === 2</ng-container>
</ng-template>
<ng-template #else1>else</ng-template>
Run Code Online (Sandbox Code Playgroud)

请注意,当条件涉及不同的变量时,它应该像正确的else if语句一样(一次只有1个情况).在这种情况下,其他一些答案无效.

放在一边:gosh angular,这是一些非常难看的else if模板代码......


Sin*_*tfi 14

您可以使用基于sitaution的多种方式:

  1. 如果您将Variable限制为特定的NumberString,最好的方法是使用ngSwitch或ngIf:

    <!-- foo = 3 -->
    <div [ngSwitch]="foo">
        <div *ngSwitchCase="1">First Number</div>
        <div *ngSwitchCase="2">Second Number</div>
        <div *ngSwitchCase="3">Third Number</div>
        <div *ngSwitchDefault>Other Number</div>
    </div>
    
    <!-- foo = 3 -->
    <ng-template [ngIf]="foo === 1">First Number</ng-template>
    <ng-template [ngIf]="foo === 2">Second Number</ng-template>
    <ng-template [ngIf]="foo === 3">Third Number</ng-template>
    
    
    <!-- foo = 'David' -->
    <div [ngSwitch]="foo">
        <div *ngSwitchCase="'Daniel'">Daniel String</div>
        <div *ngSwitchCase="'David'">David String</div>
        <div *ngSwitchCase="'Alex'">Alex String</div>
        <div *ngSwitchDefault>Other String</div>
    </div>
    
    <!-- foo = 'David' -->
    <ng-template [ngIf]="foo === 'Alex'">Alex String</ng-template>
    <ng-template [ngIf]="foo === 'David'">David String</ng-template>
    <ng-template [ngIf]="foo === 'Daniel'">Daniel String</ng-template>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 以上不适用于if elseif else代码和动态代码,您可以使用以下代码:

    <!-- foo = 5 -->
    <ng-container *ngIf="foo >= 1 && foo <= 3; then t13"></ng-container>
    <ng-container *ngIf="foo >= 4 && foo <= 6; then t46"></ng-container>
    <ng-container *ngIf="foo >= 7; then t7"></ng-container>
    
    <!-- If Statement -->
    <ng-template #t13>
        Template for foo between 1 and 3
    </ng-template>
    <!-- If Else Statement -->
    <ng-template #t46>
        Template for foo between 4 and 6
    </ng-template>
    <!-- Else Statement -->
    <ng-template #t7>
        Template for foo greater than 7
    </ng-template>
    
    Run Code Online (Sandbox Code Playgroud)

注意:您可以选择任何格式,但请注意每个代码都有自己的问题


Max*_*x21 10

为了避免嵌套和 ngSwitch,还有这种可能性,它利用了 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>
Run Code Online (Sandbox Code Playgroud)


Ger*_*hes 7

或者也许只是使用带有三元运算符的条件链。if … else if?… else if … else链。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator#Conditional_chains

<ng-container *ngIf="isFirst ? first: isSecond ? second : third"></ng-container>

<ng-template #first></ng-template>
<ng-template #second></ng-template>
<ng-template #third></ng-template>
Run Code Online (Sandbox Code Playgroud)

我更喜欢这种方法。


小智 5

如果你使用 ng-container 则不需要使用 *ngIf

<ng-container [ngTemplateOutlet]="myTemplate === 'first' ? first : myTemplate === 
   'second' ? second : third"></ng-container>

  <ng-template #first>first</ng-template>
  <ng-template #second>second</ng-template>
  <ng-template #third>third</ng-template>
Run Code Online (Sandbox Code Playgroud)