2016-08-13 63 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将作为范围变量来处理。它们的值(如果定义)将被使用。