数字过滤器最多可将数字过滤到两位小数,但不会以这种方式显示 [英] Number filter filters number down to two decimals, but does not display it that way
问题描述
这是我看到的:
该值设置为160.90
,但显示为160.8999999999
等.
The value is set at 160.90
, but displays as 160.8999999999
etc.
<input class="form-control" ng-model="imprint.total"
value="{{imprint.total | number:2}}" readonly>
它经过对某些输入的过滤来获得总数,但从本质上讲,它只是价格乘以数量.
It goes through filtering of certain inputs to get that total, but essentially it's just a price multiplied to quantity.
推荐答案
在设置属性值时,ng-model
指令将覆盖value属性中的值.您有一个只读文本框,也可以从其中删除ng-model.
The value in the value attribute will be overridden by ng-model
directive when it sets the viewvalue as it renders. You have a readonly textbox you could just as well remove the ng-model from it.
<input class="form-control"
value="{{imprint.total | number:2}}" readonly>
使用ng-model
并格式化实时数据输入,您将需要创建指令并使用解析器/格式化程序以格式化该值.
With ng-model
and to format live data entry you would need to create a directive and use parsers/formatters to format the value.
angular.module('app', []).directive('format', ['numberFilter',
function(numberFilter) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attr, ngModel) {
var decPlaces = attr.format || 2;
function formatter(value) {
if (value) {
return numberFilter(value, decPlaces);
}
}
ngModel.$formatters.push(formatter);
}
}
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-init="imprint=164.899999">
<input class="form-control" value="{{imprint | number:2}}" readonly>
<input class="form-control" ng-model="imprint" format="2" readonly>
</div>
这篇关于数字过滤器最多可将数字过滤到两位小数,但不会以这种方式显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!