这是一个完整的指令,它将对输入进行解析、格式化和执行 Angular 验证。(针对角度 1.2 和 1.3 进行测试。)
我们使用它,以便我们与服务器之间的数据模型可以用十进制表示法 (0.7634) 表示,但我们向用户提供人类可读的格式 (76.34),并强制执行最大精度。请注意,该指令仅关注数字方面。我发现单独在模板中插入“%”比在此处包含它更容易。
它默认强制执行从 -100 到 100 的输入值,但您可以使用 attrspct-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