ここでは、構文解析、フォーマット、の完全な指令があり、入力の角度検証を実行します。 (角度1.2でテストされた& 1.3)
私たちのデータモデルとサーバーとの間の乗数を10進表記(0.7634)で表現することができるようにこれを使用しますが、ユーザーには人間が判読可能な形式(76.34)最大の精度を実現します。このディレクティブは、純粋に数値的な側面に関係していることに注意してください。私はそれをここに含めるのではなく、別々にテンプレートに '%'を挿入する方が簡単だと分かります。
デフォルトでは-100〜100の入力値が適用されますが、attrs pct-min
とpct-max
を使用して独自の境界を指定できます。
'use strict';
angular.module('XLDirectives')
.directive('xlPercentage', function($filter) {
// A directive for both formatting and properly validating a percentage value.
// Assumes that our internal model is expressed as floats -1 to +1: .099 is 9.9%
// Formats display into percents 1-100, and parses user inputs down to the model.
// Parses user input as floats between 0 and 100 into floats less than 1.
// Validates user input to be within the range -100 to +100.
// Sets Angular $valid property accordingly on the ngModelController.
// If a `pct-max` or `pct-min` attribute is specified on the <input>, will use those bounds instead.
// If a `pct-decimals` attr present, will truncate inputs accordingly.
function outputFormatter(modelValue, decimals) {
var length = decimals || 2;
if (modelValue != null) {
return $filter('number')(parseFloat(modelValue) * 100, length);
} else {
return undefined;
}
};
function inputParser(viewValue, decimals) {
var length = decimals || 4;
if (viewValue != null) {
return $filter('number')(parseFloat(viewValue)/100, length);
} else {
return undefined;
}
}
function isWithinBounds(value, upper, lower) {
if (value >= lower && value <= upper) {
return true;
} else {
return false;
}
}
return {
restrict: 'A',
require: 'ngModel',
link: function postLink(scope, element, attrs, ctrl) {
ctrl.$parsers.unshift(function(viewValue) {
// confirm the input from the view contains numbers, before parsing
var numericStatus = viewValue.match(/(\d+)/),
min = parseFloat(attrs.pctMin) || -100,
max = parseFloat(attrs.pctMax) || 100,
decimals = parseFloat(attrs.pctDecimals) || 4,
bounded = isWithinBounds(viewValue, max, min);
if (numericStatus !== null && bounded) {
ctrl.$setValidity('percentage', true);
// round to max four digits after decimal
return inputParser(viewValue, decimals);
} else {
ctrl.$setValidity('percentage', false);
return undefined
}
});
ctrl.$formatters.unshift(outputFormatter);
// we have to watch for changes, and run the formatter again afterwards
element.on('change', function(e) {
var element = e.target;
element.value = outputFormatter(ctrl.$modelValue, 2);
});
}
};
});
// REFS:
// http://stackoverflow.com/questions/17344828/angularjs-should-i-use-a-filter-to-convert-integer-values-into-percentages
// http://stackoverflow.com/questions/13668440/how-to-make-a-percent-formatted-input-work-on-latest-angularjs
出典
2014-10-30 02:54:10
XML
ありがとう! 2番目の照会に関して、私は代わりに通貨の入力を探しています。つまり、入力をキー入力すると、コントロールは自動的にカンマを挿入します。おそらく私は通貨の入力を達成するためにあなたのフィドルからパーセントで始めるでしょう。どうもありがとう! – Hao
他のフォーマッタ/パーサがモデルを 'undefined'にすることができます(' $ filter( 'number') 'は別のフォーマッタが尋ねると' '4,00" 'を返します) ''。 "'小数点区切り文字)を指定します。 – elbaid
これは10進数ではなく文字列を返すものとしますか?どのようにして10進数を返すことができますか? –