[英]How to render elements if space is available for that element using ngFor in Angular2
[英]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.