[英]Define input type dynamically in HTML5 using Angular 2
Below code doesn't seem to be working. 下面的代码似乎不起作用。 Is it even possible in Angular 2?
在Angular 2中甚至可能吗?
<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>
Try this: 尝试这个:
<input type="{{column == 'created_date' ? 'date' : 'text'}}" class="form-control" />
Notice the quotes around date and time 注意日期和时间附近的引号
Without quotes, the date
and time
will be treated as scope variables. 不带引号的
date
和time
将被视为范围变量。 And their value(if defined) will be used. 和它们的值(如果定义)将被使用。
You seems to have a typo for string fields. 您似乎对字符串字段有错字。 Change the code to this:
将代码更改为此:
type="{{column == 'created_date' ? 'date' : 'text'}}"
Since the value of the type
variable should be a string, you need to put quotes around them otherwise, they will be treated as a variable defined in the associated scope. 由于
type
变量的值应为字符串,因此您需要在引号周围加上引号,否则会将它们视为在关联范围中定义的变量。
So the final output will be: 因此,最终输出将是:
<input type="{{column == 'created_date' ? 'date' : 'text'}}" class="form-control" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.