2017-04-03 134 views
2

我有一個表,我在一個數組迭代。在某些情況下,我想添加額外的<tr>。我在尋找這樣的事情:ngIf用於關閉/打開標籤

<table> 
    <tr *ngFor="let element in array"> 
    <td> 
     some content 
    </td> 
    //Starting block that will only be activated if some variable is true 
    </tr> 
    <tr> 
     <td> 
     some extra content 
     </td> 
//End of the block that will only be activated if some variable is true 
    </tr> 
</table> 

有沒有一種方法來創建一個巨石HTML,可以把它包裝這樣的嗎?

選項我試過到目前爲止正在改變數據結構(陣列),所以它包括我在尋找的元素,但我並不高興有額外的數據存在只是爲了顯示的目的。

+2

'* ngIf'不是一個迭代器,你需要使用'* ngFor'代替。 – Erevald

+0

這是一個錯字。這是一個* ngFor – ZanattMan

回答

3

這應該做你想要

<table> 
    <ng-container *ngFor="let element in array" 
    <tr> 
     <td> 
     some content 
     </td> 
    </tr> 
    <tr *ngIf="someVar"> 
     <td> 
     some extra content 
     </td> 
    </tr> 
    </ng-container> 
</table> 
+1

這正是我正在尋找的。非常感謝! – ZanattMan

+1

不客氣。很高興聽到你可以把它的工作:) –

-3

也許最好的辦法是與ng-repeat的工作是什麼。

實施例與NG-重複:

<table ng-controller="myCtrl"> 
    <tr ng-repeat="x in records"> 
     <td>{{x.Name}}</td> 
     <td>{{x.Country}}</td> 
    </tr> 
</table> 

伍重複使得用於在對象或數組。

看看這可以幫助你。

+1

沒有'NG-repeat'在Angular2。 –

+0

NG重複是angularjs == 1角,因此並不適用於我的問題 – ZanattMan

+0

對不起,我沒有看到它是從2角 –

相關問題