Я увидел это решение http://jsfiddle.net/gronky/GnTDJ/, и оно работает. То есть, когда вы вводите 25, он возвращается к модели как 0,25
HTML:
<script type="text/javascript" ng:autobind
src="http://code.angularjs.org/0.9.17/angular-0.9.17.js"></script>
<script>
function Main() {
this.var = '1.0000';
}
</script>
<div ng:controller="Main">
<input type="text" name="var" ng:format="percent">
<pre>var = {{var|json}}</pre>
</div>
JavaScript:
angular.formatter('percent', {
parse: function(value) {
var m = value.match(/^(\d+)\/(\d+)/);
if (m != null)
return angular.filter.number(parseInt(m[1])/parseInt(m[2]), 2);
return angular.filter.number(parseFloat(value)/100, 2);
},
format: function(value) {
return angular.filter.number(parseFloat(value)*100, 0);
},
});
Я попытался заставить его работать с последним AngularJS, он больше не работает, но http://jsfiddle.net/TrJcB/ То есть, когда вы вводите 25, также отбрасывается как 25, он не нажимает правильное значение 0,25 для модели.
Или, может быть, уже есть встроенный форматтер для процентов? Я тоже хотел форматировать форму или запятую.