简体   繁体   English

猫鼬模块小数的AngularJS输入

[英]AngularJS input from mongoose module decimals

I have a mongoose model containing a salesPrice type number. 我有一个包含salesPrice类型编号的猫鼬模型。 Since mongoose doesn't support decimals I created a setter and a getter like this 由于猫鼬不支持小数,所以我像这样创建了一个setter和一个getter

function getSalesPrice(num){
    if (num)
        return (num/100).toFixed(2);
    else
        return (0).toFixed(2);
}

function setSalesPrice(num){
    return num*100;
}

In Angular view i want to view value with decimals, so eg I store 99 in model meaning $0.99 only in my form will show as 99 在角度视图中,我想查看带小数的值,例如,我将99存储在模型中,意味着$ 0.99仅在我的表单中将显示为99

<label for="salesPrice" class="col-md-2 control-label">Salesprice</label>
<div class="col-md-10">
    <input type="number" class="form-control" data-ng-model="asset.salesData.salesPrice" id="salesPrice" placeholder="Sales price in USD">
</div>

What's way to work with this correctly? 如何正确处理此问题?

If I understand your need correctly, then the best way to solve this problem is to use angular's built-in 'currency' filter. 如果我正确理解您的需求,那么解决此问题的最佳方法是使用angular的内置“货币”过滤器。 If you have a $resource that returns values in decimal format from eg mongoose, then you can display the values like: 如果您有一个$ resource可从例如猫鼬返回十进制格式的值,则可以显示类似以下的值:

<div ng-repeat="value in values">
    {{ value | currency }}
</div>

And achieve a locale specific (based on the browser viewing it) result. 并实现特定于语言环境(基于浏览器查看它)的结果。

here is an example plnkr showing this off (without a mock resource) 是一个示例plnkr展示了这一点(没有模拟资源)

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

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