2017-08-08 56 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>

相關問題