Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 1001d76

Browse files
cironunesNarretz
authored andcommitted
feat(input): add support for binding to input[type=range]
Thanks to @cironunes for the initial implementation in #9715 Adds support for binding to input[range] with the following behavior / features: - Like input[number], it requires the model to be a Number, and will set the model to a Number - it supports setting the min/max values via the min/max and ngMin/ngMax attributes - it follows the browser behavior of never allowing an invalid value. That means, when the browser converts an invalid value (empty: null, undefined, false ..., out of bounds: greater than max, less than min) to a valid value, the input will in turn set the model to this new valid value via $setViewValue. -- this means a range input will never be required and never have a non-Number model value, once the ngModel directive is initialized. -- this behavior is supported when the model changes and when the min/max attributes change in a way that prompts the browser to update the input value. -- ngMin / ngMax do not prompt the browser to update the values, as they don't set the attribute values. Instead, they will set the min / max errors when appropriate - browsers that do not support input[range] (IE9) handle the input like a number input (with validation etc.) Closes #5892 Closes #9715 Close #14870
1 parent b4769c3 commit 1001d76

File tree

4 files changed

+642
-6
lines changed

4 files changed

+642
-6
lines changed

docs/content/error/ngModel/numfmt.ngdoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@fullName Model is not of type `number`
44
@description
55

6-
The number input directive `<input type="number">` requires the model to be a `number`.
6+
The `input[number]` and `input[range]` directives require the model to be a `number`.
77

88
If the model is something else, this error will be thrown.
99

src/ng/directive/input.js

+215-4
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,110 @@ var inputType = {
10311031
*/
10321032
'radio': radioInputType,
10331033

1034+
/**
1035+
* @ngdoc input
1036+
* @name input[range]
1037+
*
1038+
* @description
1039+
* Native range input with validation and transformation.
1040+
*
1041+
* The model for the range input must always be a `Number`.
1042+
*
1043+
* IE9 does not support the `range` type, but falls back
1044+
* to a text input. Model binding, validation and number parsing are nevertheless supported.
1045+
*
1046+
* Browsers that support range (latest Chrome, Safari, Firefox, Edge) treat `input[range]`
1047+
* in a way that never allows the input to hold an invalid value. That means:
1048+
* - any non-numerical value is set to `(max + min) / 2`.
1049+
* - any numerical value that is less than the current min val, or greater than the current max val
1050+
* is set to the min / max val respectively.
1051+
*
1052+
* This has the following consequences for Angular:
1053+
*
1054+
* Since the element value should always reflect the current model value, a range input
1055+
* will set the bound ngModel expression to the value that the browser has set for the
1056+
* input element. For example, in the following input `<input type="range" ng-model="model.value">`,
1057+
* if the application sets `model.value = null`, the browser will set the input to `'50'`.
1058+
* Angular will then set the model to `50`, to prevent input and model value being out of sync.
1059+
*
1060+
* This does not only affect changes to the model value, but also to the values of the `min` and
1061+
* `max` attributes. When these change in a way that will cause the browser to modify the input value,
1062+
* Angular will also update the model value.
1063+
*
1064+
* Automatic value adjustment also means that a range input element can never have the `required`,
1065+
* `min`, or `max` errors, except when using `ngMax` and `ngMin`, which are not affected by automatic
1066+
* value adjustment, because they do not set the `min` and `max` attributes.
1067+
*
1068+
* @param {string} ngModel Assignable angular expression to data-bind to.
1069+
* @param {string=} name Property name of the form under which the control is published.
1070+
* @param {string=} min Sets the `min` validation to ensure that the value entered is greater
1071+
* than `min`. Can be interpolated.
1072+
* @param {string=} max Sets the `max` validation to ensure that the value entered is less than `max`.
1073+
* Can be interpolated.
1074+
* @param {string=} ngMin Takes an expression. Sets the `min` validation to ensure that the value
1075+
* entered is greater than `min`. Does not set the `min` attribute and therefore
1076+
* adds no native HTML5 validation. It also means the browser won't adjust the
1077+
* element value in case `min` is greater than the current value.
1078+
* @param {string=} ngMax Takes an expression. Sets the `max` validation to ensure that the value
1079+
* entered is less than `max`. Does not set the `max` attribute and therefore
1080+
* adds no native HTML5 validation. It also means the browser won't adjust the
1081+
* element value in case `max` is less than the current value.
1082+
* @param {string=} ngChange Angular expression to be executed when the ngModel value changes due
1083+
* to user interaction with the input element.
1084+
*
1085+
* @example
1086+
<example name="range-input-directive" module="rangeExample">
1087+
<file name="index.html">
1088+
<script>
1089+
angular.module('rangeExample', [])
1090+
.controller('ExampleController', ['$scope', function($scope) {
1091+
$scope.value = 75;
1092+
$scope.min = 10;
1093+
$scope.max = 90;
1094+
}]);
1095+
</script>
1096+
<form name="myForm" ng-controller="ExampleController">
1097+
1098+
Model as range: <input type="range" name="range" ng-model="value" min="{{min}}" max="{{max}}">
1099+
<hr>
1100+
Model as number: <input type="number" ng-model="value"><br>
1101+
Min: <input type="number" ng-model="min"><br>
1102+
Max: <input type="number" ng-model="min"><br>
1103+
value = <code>{{value}}</code><br/>
1104+
myForm.range.$valid = <code>{{myForm.range.$valid}}</code><br/>
1105+
myForm.range.$error = <code>{{myForm.range.$error}}</code>
1106+
</form>
1107+
</file>
1108+
</example>
1109+
1110+
* ## Range Input with ngMin & ngMax attributes
1111+
1112+
* @example
1113+
<example name="range-input-directive-ng" module="rangeExample">
1114+
<file name="index.html">
1115+
<script>
1116+
angular.module('rangeExample', [])
1117+
.controller('ExampleController', ['$scope', function($scope) {
1118+
$scope.value = 75;
1119+
$scope.min = 10;
1120+
$scope.max = 90;
1121+
}]);
1122+
</script>
1123+
<form name="myForm" ng-controller="ExampleController">
1124+
Model as range: <input type="range" name="range" ng-model="value" ng-min="min" ng-max="max">
1125+
<hr>
1126+
Model as number: <input type="number" ng-model="value"><br>
1127+
Min: <input type="number" ng-model="min"><br>
1128+
Max: <input type="number" ng-model="min"><br>
1129+
value = <code>{{value}}</code><br/>
1130+
myForm.range.$valid = <code>{{myForm.range.$valid}}</code><br/>
1131+
myForm.range.$error = <code>{{myForm.range.$error}}</code>
1132+
</form>
1133+
</file>
1134+
</example>
1135+
1136+
*/
1137+
'range': rangeInputType,
10341138

10351139
/**
10361140
* @ngdoc input
@@ -1382,10 +1486,7 @@ function badInputChecker(scope, element, attr, ctrl) {
13821486
}
13831487
}
13841488

1385-
function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1386-
badInputChecker(scope, element, attr, ctrl);
1387-
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);
1388-
1489+
function numberFormatterParser(ctrl) {
13891490
ctrl.$$parserName = 'number';
13901491
ctrl.$parsers.push(function(value) {
13911492
if (ctrl.$isEmpty(value)) return null;
@@ -1402,6 +1503,12 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
14021503
}
14031504
return value;
14041505
});
1506+
}
1507+
1508+
function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1509+
badInputChecker(scope, element, attr, ctrl);
1510+
numberFormatterParser(ctrl);
1511+
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);
14051512

14061513
if (isDefined(attr.min) || attr.ngMin) {
14071514
var minVal;
@@ -1436,6 +1543,110 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
14361543
}
14371544
}
14381545

1546+
function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1547+
badInputChecker(scope, element, attr, ctrl);
1548+
numberFormatterParser(ctrl);
1549+
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);
1550+
1551+
var minVal = 0,
1552+
maxVal = 100,
1553+
supportsRange = ctrl.$$hasNativeValidators && element[0].type === 'range',
1554+
validity = element[0].validity;
1555+
1556+
var originalRender = ctrl.$render;
1557+
1558+
ctrl.$render = supportsRange && isDefined(validity.rangeUnderflow) && isDefined(validity.rangeOverflow) ?
1559+
//Browsers that implement range will set these values automatically, but reading the adjusted values after
1560+
//$render would cause the min / max validators to be applied with the wrong value
1561+
function rangeRender() {
1562+
originalRender();
1563+
ctrl.$setViewValue(element.val());
1564+
} :
1565+
originalRender;
1566+
1567+
function minChange(val) {
1568+
if (isDefined(val) && !isNumber(val)) {
1569+
val = parseFloat(val);
1570+
}
1571+
minVal = isNumber(val) && !isNaN(val) ? val : undefined;
1572+
// ignore changes before model is initialized
1573+
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
1574+
return;
1575+
}
1576+
1577+
if (supportsRange && minAttrType === 'min') {
1578+
var elVal = element.val();
1579+
// IE11 doesn't set the el val correctly if the minVal is greater than the element value
1580+
if (minVal > elVal) {
1581+
element.val(minVal);
1582+
elVal = minVal;
1583+
}
1584+
ctrl.$setViewValue(elVal);
1585+
} else {
1586+
// TODO(matsko): implement validateLater to reduce number of validations
1587+
ctrl.$validate();
1588+
}
1589+
}
1590+
1591+
var minAttrType = isDefined(attr.ngMin) ? 'ngMin' : isDefined(attr.min) ? 'min' : false;
1592+
if (minAttrType) {
1593+
ctrl.$validators.min = isDefined(attr.min) && supportsRange ?
1594+
function noopMinValidator(value) {
1595+
// Since all browsers set the input to a valid value, we don't need to check validity
1596+
return true;
1597+
} :
1598+
// ngMin doesn't set the min attr, so the browser doesn't adjust the input value as setting min would
1599+
function minValidator(modelValue, viewValue) {
1600+
return ctrl.$isEmpty(viewValue) || isUndefined(minVal) || viewValue >= minVal;
1601+
};
1602+
1603+
// Assign minVal when the directive is linked. This won't run the validators as the model isn't ready yet
1604+
minChange(attr.min);
1605+
attr.$observe('min', minChange);
1606+
}
1607+
1608+
function maxChange(val) {
1609+
if (isDefined(val) && !isNumber(val)) {
1610+
val = parseFloat(val);
1611+
}
1612+
maxVal = isNumber(val) && !isNaN(val) ? val : undefined;
1613+
// ignore changes before model is initialized
1614+
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
1615+
return;
1616+
}
1617+
1618+
if (supportsRange && maxAttrType === 'max') {
1619+
var elVal = element.val();
1620+
// IE11 doesn't set the el val correctly if the maxVal is less than the element value
1621+
if (maxVal < elVal) {
1622+
element.val(maxVal);
1623+
elVal = minVal;
1624+
}
1625+
ctrl.$setViewValue(elVal);
1626+
} else {
1627+
// TODO(matsko): implement validateLater to reduce number of validations
1628+
ctrl.$validate();
1629+
}
1630+
}
1631+
var maxAttrType = isDefined(attr.max) ? 'max' : attr.ngMax ? 'ngMax' : false;
1632+
if (maxAttrType) {
1633+
ctrl.$validators.max = isDefined(attr.max) && supportsRange ?
1634+
function noopMaxValidator() {
1635+
// Since all browsers set the input to a valid value, we don't need to check validity
1636+
return true;
1637+
} :
1638+
// ngMax doesn't set the max attr, so the browser doesn't adjust the input value as setting max would
1639+
function maxValidator(modelValue, viewValue) {
1640+
return ctrl.$isEmpty(viewValue) || isUndefined(maxVal) || viewValue <= maxVal;
1641+
};
1642+
1643+
// Assign maxVal when the directive is linked. This won't run the validators as the model isn't ready yet
1644+
maxChange(attr.max);
1645+
attr.$observe('max', maxChange);
1646+
}
1647+
1648+
}
1649+
14391650
function urlInputType(scope, element, attr, ctrl, $sniffer, $browser) {
14401651
// Note: no badInputChecker here by purpose as `url` is only a validation
14411652
// in browsers, i.e. we can always read out input.value even if it is not valid!

src/ng/directive/ngModel.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,8 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
881881
ctrl.$viewValue = ctrl.$$lastCommittedViewValue = viewValue;
882882
ctrl.$render();
883883

884-
ctrl.$$runValidators(modelValue, viewValue, noop);
884+
// It is possible that model and view value have been updated during render
885+
ctrl.$$runValidators(ctrl.$modelValue, ctrl.$viewValue, noop);
885886
}
886887
}
887888

0 commit comments

Comments
 (0)