我从数据库获取的值为10.00,520.00但是当我绑定到输入时显示为10和520
例如
<input type="text" ng-model="Amount">
$scope.Amount =10.00 ;
将值绑定到文本框的值为10,但我想绑定10.00
答案 0 :(得分:4)
检查以下工作代码。
var app = angular.module('plunker', []);
app.directive('validNumber', function () {
return {
link: function (scope, element, attrs, ngModelCtrl) {
setTimeout(function () {
var digits = element.val().split('.')[1];
digits = digits == null ? 2 : digits.length;
element.val(parseFloat(element.val()).toFixed(digits));
}, 100);
}
};
})
app.controller('MainCtrl', function ($scope) {
$scope.Amount = 10.0001;
});
&#13;
<script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
<div ng-app="plunker" ng-controller="MainCtrl">
<input type="text" ng-model="Amount" valid-number />
{{Amount}}
</div>
&#13;
答案 1 :(得分:1)