角度转换选择值到 int [英] Angular cast select value to int
本文介绍了角度转换选择值到 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有不同选择的表单,例如:
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>
我的后端希望在isConnected"属性中收到一个整数.不幸的是,一旦我更改了 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
}
对于标准 我可以使用
type="number"
但是对于 我很无能.
For standard <input>
I could use type="number"
but for a <select>
I'm clueless.
有没有办法强制 angular 2 将数据转换为 int ?
Is there a way to force angular 2 to cast the data to int ?
推荐答案
使用 [ngValue] 代替value":
Use [ngValue] instead of "value":
<select [(ngModel)]="selected.isConnected" id="etat">
<option [ngValue]="0">Not connected</option>
<option [ngValue]="1">Connected</option>
</select>
这篇关于角度转换选择值到 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文