简体   繁体   English

类型的ionic html输入字段

[英]ionic html input field of type number

Even though defining an input field as number still return as a string in the component 即使将输入字段定义为数字,组件中的字符串仍然返回

<ion-input [(ngModel)]="salary" 
      (ngModelChange)="calc($event)"
       required
      type="number" 
      name="salary">
</ion-input>

In the component as well the field is defined as a number 在组件中,该字段也定义为数字

private salary: number;

Why does not it return as a number? 为什么它不返回数字?

Thanks 谢谢

This is a known issue . 这是一个已知问题

As a workaround, you can use the ionChange event on the input to transform the value to a number, and then assign it to a class variable. 解决方法是,可以在输入上使用ionChange事件将值转换为数字,然后将其分配给类变量。

In your template 在您的模板中

<ion-input [ngModel]="salary"
      (ionChange)="transform($event)"
      required
      type="number" 
      name="salary">
</ion-input>

In your component 在你的组件中

transform(event) {
  // Multiply by 1 to transform to number
  this.salary = event.value * 1;
}

Another Work Around: 另一个解决方法:

private salary: number;

Instead Of type number specify it as , string : 代替类型号,将其指定为, 字符串

private salary: string;

Then let ion-input return string 然后让离子输入返回字符串

cast it it integer or float according to your requirements like this: 根据您的要求将其强制转换为整数浮点型 ,如下所示:

parseInt(salary) 

if you cross check using typeOf it will give number . 如果您使用typeOf进行交叉检查,它将给出number

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM