我正在使用这样的 ngx-mask:
<input type="text" class="form-control"
id="price"
name="price"
[(ngModel)]="product.maskPrice"
mask="separator.2"
thousandSeparator="."
>
所以我必须将此输入转换为数字,例如:1234,56 到 1234.56,因为我的端点除了价格的数字类型。
这是我的代码:
product.price = +product.maskPrice!.replace(",",'.');
最好的方法是如何做到这一点?
我的解决方案使用这个接口来保存掩码和数字价格信息:
export interface Product {
id ?: number;
name ?: string;
price ?: number;
maskPrice ?: string;
newProduct ?: boolean;
}
有什么建议可以改进此代码吗?谢谢!