2016-08-13 40 views
1

下面的代碼似乎不起作用。在Angular 2中甚至有可能嗎?使用Angular 2在HTML5中動態定義輸入類型

<table class="table table-responsive" style="border:0"> 
    <tr *ngFor="#column of columns" style="height:20px;"> 
     <td class="text-right" style="padding-top:10px;border:0"> 
      <h4> {{column | case}}: </h4> 
     </td> 
     <td class="text-center" style="padding-top:10px;border:0"> 
     <input type="{{column == 'created_date' ? date : text}}" class="form-control" /> 
     </td> 
    </tr> 
</table> 

回答

1

您似乎有字符串字段的拼寫錯誤。代碼改成這樣:

type="{{column == 'created_date' ? 'date' : 'text'}}" 

由於type變量的值應該是一個字符串,你需要把報價身邊,否則,他們將被視爲在相關範圍內定義的變量。

所以最終的結果應該是:

<input type="{{column == 'created_date' ? 'date' : 'text'}}" class="form-control" /> 
+0

笑我太愚蠢!謝謝 – user728630

1

試試這個:

<input type="{{column == 'created_date' ? 'date' : 'text'}}" class="form-control" /> 

通知周圍的日期和時間

報價不帶引號的datetime將作爲範圍變量來處理。它們的值(如果定義)將被使用。