Angular 2将选择值转换为int [英] Angular 2 cast select value to int
本文介绍了Angular 2将选择值转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< select [(ngModel)] =selected.isConnected (ngModelChange)=formChanged()name =etatid =etatclass =form-control>
< option value =0>未连接< / option>
< option value =1>连线< / option>
< / select>
我的后端希望在isConnected属性中接收一个int。不幸的是,只要我改变select的值,这个属性就会被转换为一个字符串:
{
isConnected: 0,// 0预计
}
对于标准 < input>
我可以使用 type =number
,但对于< select>
我很笨。
有没有办法强制angular 2将数据转换为int?
< select [ (ngModel)] =selected.isConnectedid =etat>
< option [ngValue] =0>未连接< / option>
< option [ngValue] =1>连线< / option>
< / select>
I have a form with different selects like :
<select [(ngModel)]="selected.isConnected" (ngModelChange)="formChanged()" name="etat" id="etat" class="form-control">
<option value="0">Not connected</option>
<option value="1">Connected</option>
</select>
My backend expect to receive an int in the "isConnected" attribute. Unfortunately as soon as I change the value of the select the attribute is cast to a string :
{
isConnected : "0", // 0 expected
}
For standard <input>
I could use type="number"
but for a <select>
I'm clueless.
Is there a way to force angular 2 to cast the data to int ?
解决方案
Use [ngValue] instead of "value":
<select [(ngModel)]="selected.isConnected" id="etat">
<option [ngValue]="0">Not connected</option>
<option [ngValue]="1">Connected</option>
</select>
这篇关于Angular 2将选择值转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文