将角度强制选择值转换为int [英] Angular cast select value to int

查看:49
本文介绍了将角度强制选择值转换为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
}

对于标准<input>,我可以使用type="number",但对于<select>,我却一无所知.

For standard <input> I could use type="number" but for a <select> I'm clueless.

是否有一种方法可以强制角度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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆