角度ng-模型表达

时间:2015-05-30 06:11:30

标签: html angularjs math rounding angularjs-ng-model

我想问一下如何使用 angularjs

对输入中的数字进行舍入
<input type="number" ng-model="value1"/>

我想要用舍入来显示2位小数。

你能帮忙吗

2 个答案:

答案 0 :(得分:1)

您可以使用ng-change:

<input type="number" ng-model="value1" ng-change="roundNumber()" />

$scope.roundNumber = function(){
    $scope.value1 = Math.round($scope.value1 * 100) / 100;
};

//call function once at bottom of controller for initial run
$scope.roundNumber();

如果这将是您想要的常用功能,则创建一个指令来执行此操作

答案 1 :(得分:1)

在这种情况下使用$watch会更合适,它也会在初始加载时格式化值。

<强>标记

<input type="number" ng-model="value1"/>

<强>代码

$scope.$watch('value1',function(newVal, oldVal){
    if((newVal != oldVal))
       $scope.value1 = newVal?  newVal.toFixed(2): 0;
});
相关问题