2017-08-08 58 views
1

我想输入值0.00到文本框中 绑定的话,如果我改变输入到20然后在模糊集20.00如何默认十进制值设置为输入angularjs

<input type="text" valid-decimal-number style="text-align:right" ng-model="Amount" ng-blur="ConvertToDecimal(Amount)"> 

角脚本:

//converting value to decimal 
$scope.ConvertToDecimal = function (val) { 
    $scope.Amount = eval(val).toFixed(2); 
} 

我通过使用此代码越来越但是,对于每一次我写信, 如何创建此

定制指令

我也试过这样也不行。

<input type="text" valid-decimal-number style="text-align:right" ng-model="Amount" ng-blur="'Amount=(Amount).toFixed(2)'"> 
+1

从纳克模糊 – Vivz

+0

删除“我已经尝试删除也,文本框成为空模糊事件 – Srinu

回答

1

使用ng-blur

ng-blur="Amount = (Amount | number : 2)" 

演示过滤

angular.module("app",[]) 
 
.controller("ctrl",function($scope){ 
 

 

 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
<input type="text" style="text-align:right" ng-model="Amount" ng-blur="Amount = (Amount | number : 2)" > 
 

 
</div>

+0

后谢谢sachila,其worked.Is有可能为此创建自定义指令 – Srinu

1

您可以申请toFixed(2)为数字仅

<html ng-app="myApp"> 
 
<head> 
 
\t <title></title> 
 
</head> 
 
<body ng-controller="myCtrl"> 
 
<input type="text" valid-decimal-number style="text-align:right" ng-model="Amount" ng-blur="ConvertToDecimal()"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script type="text/javascript"> 
 
var app = angular.module("myApp", []); 
 
app.controller('myCtrl', ['$scope', function ($scope) { 
 
$scope.ConvertToDecimal = function (val) { 
 
\t if(isNaN($scope.Amount)){ 
 
\t \t alert("given input is not a number"); 
 
\t \t return; 
 
\t } 
 
     $scope.Amount = Number($scope.Amount).toFixed(2); 
 
    } 
 
}]); 
 
</script> 
 
</body> 
 
</html>

相关问题