选项标签返回字符串值,而不是Angular中ngModel的数字 [英] option tag returning string value instead of number for ngModel in Angular

查看:68
本文介绍了选项标签返回字符串值,而不是Angular中ngModel的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML:

<select [(ngModel)]='i.PaycodeId'>
  <option *ngFor="let j of payCode" [value]='j.ID'>{{j.value}}</option>
</select>

payCode:

[{"ID":0,"value":"Cycle"},{"ID":1,"value":"Truck"},{"ID":2,"value":"Car"}]

i.PaycodeId中,通过ngModel将数字设置为字符串值,而不是像"1"/"2"这样的数字,而传递给select的值是将ID的值作为数字的对象. 我只想将此值作为数字.

In i.PaycodeId, through ngModel, it is setting numbers as string value not numbers like "1"/"2" while the value passed to select is object having ID's value as number. I want this value as number only.

推荐答案

您可以像这样将绑定分开:

You could seperate the binding out like this:

<select [ngModel]="i.PaycodeId" (ngModelChange)="onChangeSelection($event)">
  <option *ngFor="let j of payCode" [value]='j.ID'>{{j.value}}</option>
</select>

在您的组件中:

onChangeSelection(selected) {
    this.i.PaycodeId = parseInt(selected);
}


使用柱塞为例

这篇关于选项标签返回字符串值,而不是Angular中ngModel的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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