簡體   English   中英

如何基於angular2條件使用ngFor渲染不同的項目

[英]how to render different items using ngFor based on a condition angular2

我必須使用ngFor動態創建一些html組件。 需要基於json的值呈現文本框,選擇等不同的項目。

我知道如何使用for循環和if條件在javascript中執行此操作。 但是角度2的替代項是什么?

例如

for(var i=0;i<data.length;i++){
    if(data.type=="textbox"){
      $('#container').append(//textbox snippet)
    }

    if(data.type=="select"){
      $('#container').append(//select snippet)
    }
}

角度2的替代方法是什么? 任何幫助,不勝感激。

您可以使用*ngIf指令。

例如 :

<div *ngIf="(data.type == 'textbox')">
<!--textbox snippet here-->
</div>

要么

<input *ngIf="(data.type == 'textbox')" type="text" value="" />

這是有關*ngIf指令的文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM