Skip to content

Commit 4dbaa07

Browse files
revert:feat(input): add support for binding to input[type=range]
This reverts commit 296da4b
1 parent 198fbff commit 4dbaa07

File tree

4 files changed

+8
-709
lines changed

4 files changed

+8
-709
lines changed

docs/content/error/ngModel/numfmt.ngdoc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
@fullName Model is not of type `number`
44
@description
55

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

98
If the model is something else, this error will be thrown.
109

src/ng/directive/input.js

Lines changed: 6 additions & 243 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,132 +1027,6 @@ var inputType = {
10271027
*/
10281028
'radio': radioInputType,
10291029

1030-
/**
1031-
* @ngdoc input
1032-
* @name input[range]
1033-
*
1034-
* @description
1035-
* Native range input with validation and transformation.
1036-
*
1037-
* <div class="alert alert-warning">
1038-
* <p>
1039-
* In v1.5.9+, in order to avoid interfering with already existing, custom directives for
1040-
* `input[range]`, you need to let Angular know that you want to enable its built-in support.
1041-
* You can do this by adding the `ng-input-range` attribute to the input element. E.g.:
1042-
* `<input type="range" ng-input-range ... />`
1043-
* </p><br />
1044-
* <p>
1045-
* Input elements without the `ng-input-range` attibute will continue to be treated the same
1046-
* as in previous versions (e.g. their model value will be a string not a number and Angular
1047-
* will not take `min`/`max`/`step` attributes and properties into account).
1048-
* </p><br />
1049-
* <p>
1050-
* **Note:** From v1.6.x onwards, the support for `input[range]` will be always enabled and
1051-
* the `ng-input-range` attribute will have no effect.
1052-
* </p><br />
1053-
* <p>
1054-
* This documentation page refers to elements which have the built-in support enabled; i.e.
1055-
* elements _with_ the `ng-input-range` attribute.
1056-
* </p>
1057-
* </div>
1058-
*
1059-
* The model for the range input must always be a `Number`.
1060-
*
1061-
* IE9 and other browsers that do not support the `range` type fall back
1062-
* to a text input. Model binding, validation and number parsing are nevertheless supported.
1063-
*
1064-
* Browsers that support range (latest Chrome, Safari, Firefox, Edge) treat `input[range]`
1065-
* in a way that never allows the input to hold an invalid value. That means:
1066-
* - any non-numerical value is set to `(max + min) / 2`.
1067-
* - any numerical value that is less than the current min val, or greater than the current max val
1068-
* is set to the min / max val respectively.
1069-
*
1070-
* This has the following consequences for Angular:
1071-
*
1072-
* Since the element value should always reflect the current model value, a range input
1073-
* will set the bound ngModel expression to the value that the browser has set for the
1074-
* input element. For example, in the following input `<input type="range" ng-input-range ng-model="model.value">`,
1075-
* if the application sets `model.value = null`, the browser will set the input to `'50'`.
1076-
* Angular will then set the model to `50`, to prevent input and model value being out of sync.
1077-
*
1078-
* That means the model for range will immediately be set to `50` after `ngModel` has been
1079-
* initialized. It also means a range input can never have the required error.
1080-
*
1081-
* This does not only affect changes to the model value, but also to the values of the `min` and
1082-
* `max` attributes. When these change in a way that will cause the browser to modify the input value,
1083-
* Angular will also update the model value.
1084-
*
1085-
* Automatic value adjustment also means that a range input element can never have the `required`,
1086-
* `min`, or `max` errors.
1087-
*
1088-
* Note that `input[range]` is not compatible with`ngMax` and `ngMin`, because they do not set the
1089-
* `min` and `max` attributes, which means that the browser won't automatically adjust the input
1090-
* value based on their values, and will always assume min = 0 and max = 100.
1091-
*
1092-
* @param ngInputRange The presense of this attribute enables the built-in support for
1093-
* `input[range]`.
1094-
* @param {string} ngModel Assignable angular expression to data-bind to.
1095-
* @param {string=} name Property name of the form under which the control is published.
1096-
* @param {string=} min Sets the `min` validation to ensure that the value entered is greater
1097-
* than `min`. Can be interpolated.
1098-
* @param {string=} max Sets the `max` validation to ensure that the value entered is less than `max`.
1099-
* Can be interpolated.
1100-
* @param {string=} ngChange Angular expression to be executed when the ngModel value changes due
1101-
* to user interaction with the input element.
1102-
*
1103-
* @example
1104-
<example name="range-input-directive" module="rangeExample">
1105-
<file name="index.html">
1106-
<script>
1107-
angular.module('rangeExample', [])
1108-
.controller('ExampleController', ['$scope', function($scope) {
1109-
$scope.value = 75;
1110-
$scope.min = 10;
1111-
$scope.max = 90;
1112-
}]);
1113-
</script>
1114-
<form name="myForm" ng-controller="ExampleController">
1115-
1116-
Model as range: <input type="range" ng-input-range name="range" ng-model="value" min="{{min}}" max="{{max}}">
1117-
<hr>
1118-
Model as number: <input type="number" ng-model="value"><br>
1119-
Min: <input type="number" ng-model="min"><br>
1120-
Max: <input type="number" ng-model="max"><br>
1121-
value = <code>{{value}}</code><br/>
1122-
myForm.range.$valid = <code>{{myForm.range.$valid}}</code><br/>
1123-
myForm.range.$error = <code>{{myForm.range.$error}}</code>
1124-
</form>
1125-
</file>
1126-
</example>
1127-
1128-
* ## Range Input with ngMin & ngMax attributes
1129-
1130-
* @example
1131-
<example name="range-input-directive-ng" module="rangeExample">
1132-
<file name="index.html">
1133-
<script>
1134-
angular.module('rangeExample', [])
1135-
.controller('ExampleController', ['$scope', function($scope) {
1136-
$scope.value = 75;
1137-
$scope.min = 10;
1138-
$scope.max = 90;
1139-
}]);
1140-
</script>
1141-
<form name="myForm" ng-controller="ExampleController">
1142-
Model as range: <input type="range" ng-input-range name="range" ng-model="value" ng-min="min" ng-max="max">
1143-
<hr>
1144-
Model as number: <input type="number" ng-model="value"><br>
1145-
Min: <input type="number" ng-model="min"><br>
1146-
Max: <input type="number" ng-model="max"><br>
1147-
value = <code>{{value}}</code><br/>
1148-
myForm.range.$valid = <code>{{myForm.range.$valid}}</code><br/>
1149-
myForm.range.$error = <code>{{myForm.range.$error}}</code>
1150-
</form>
1151-
</file>
1152-
</example>
1153-
1154-
*/
1155-
'range': rangeInputType,
11561030

11571031
/**
11581032
* @ngdoc input
@@ -1504,7 +1378,10 @@ function badInputChecker(scope, element, attr, ctrl) {
15041378
}
15051379
}
15061380

1507-
function numberFormatterParser(ctrl) {
1381+
function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1382+
badInputChecker(scope, element, attr, ctrl);
1383+
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);
1384+
15081385
ctrl.$$parserName = 'number';
15091386
ctrl.$parsers.push(function(value) {
15101387
if (ctrl.$isEmpty(value)) return null;
@@ -1521,17 +1398,9 @@ function numberFormatterParser(ctrl) {
15211398
}
15221399
return value;
15231400
});
1524-
}
1525-
1526-
function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1527-
badInputChecker(scope, element, attr, ctrl);
1528-
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);
1529-
numberFormatterParser(ctrl);
1530-
1531-
var minVal;
1532-
var maxVal;
15331401

15341402
if (isDefined(attr.min) || attr.ngMin) {
1403+
var minVal;
15351404
ctrl.$validators.min = function(value) {
15361405
return ctrl.$isEmpty(value) || isUndefined(minVal) || value >= minVal;
15371406
};
@@ -1547,6 +1416,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15471416
}
15481417

15491418
if (isDefined(attr.max) || attr.ngMax) {
1419+
var maxVal;
15501420
ctrl.$validators.max = function(value) {
15511421
return ctrl.$isEmpty(value) || isUndefined(maxVal) || value <= maxVal;
15521422
};
@@ -1562,113 +1432,6 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15621432
}
15631433
}
15641434

1565-
function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1566-
badInputChecker(scope, element, attr, ctrl);
1567-
numberFormatterParser(ctrl);
1568-
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);
1569-
1570-
var supportsRange = ctrl.$$hasNativeValidators && element[0].type === 'range',
1571-
minVal = supportsRange ? 0 : undefined,
1572-
maxVal = supportsRange ? 100 : undefined,
1573-
validity = element[0].validity,
1574-
hasMinAttr = isDefined(attr.min),
1575-
hasMaxAttr = isDefined(attr.max);
1576-
1577-
var originalRender = ctrl.$render;
1578-
1579-
ctrl.$render = supportsRange && isDefined(validity.rangeUnderflow) && isDefined(validity.rangeOverflow) ?
1580-
//Browsers that implement range will set these values automatically, but reading the adjusted values after
1581-
//$render would cause the min / max validators to be applied with the wrong value
1582-
function rangeRender() {
1583-
originalRender();
1584-
ctrl.$setViewValue(element.val());
1585-
} :
1586-
originalRender;
1587-
1588-
if (hasMinAttr) {
1589-
ctrl.$validators.min = supportsRange ?
1590-
// Since all browsers set the input to a valid value, we don't need to check validity
1591-
function noopMinValidator() { return true; } :
1592-
// non-support browsers validate the range
1593-
function minValidator(modelValue, viewValue) {
1594-
return ctrl.$isEmpty(viewValue) || isUndefined(minVal) || viewValue >= minVal;
1595-
};
1596-
1597-
setInitialValueAndObserver('min', minChange);
1598-
}
1599-
1600-
if (hasMaxAttr) {
1601-
ctrl.$validators.max = supportsRange ?
1602-
// Since all browsers set the input to a valid value, we don't need to check validity
1603-
function noopMaxValidator() { return true; } :
1604-
// ngMax doesn't set the max attr, so the browser doesn't adjust the input value as setting max would
1605-
function maxValidator(modelValue, viewValue) {
1606-
return ctrl.$isEmpty(viewValue) || isUndefined(maxVal) || viewValue <= maxVal;
1607-
};
1608-
1609-
setInitialValueAndObserver('max', maxChange);
1610-
}
1611-
1612-
function setInitialValueAndObserver(htmlAttrName, changeFn) {
1613-
// interpolated attributes set the attribute value only after a digest, but we need the
1614-
// attribute value when the input is first rendered, so that the browser can adjust the
1615-
// input value based on the min/max value
1616-
element.attr(htmlAttrName, attr[htmlAttrName]);
1617-
1618-
attr.$observe(htmlAttrName, changeFn);
1619-
}
1620-
1621-
function minChange(val) {
1622-
if (isDefined(val) && !isNumber(val)) {
1623-
val = parseFloat(val);
1624-
}
1625-
minVal = isNumber(val) && !isNaN(val) ? val : undefined;
1626-
// ignore changes before model is initialized
1627-
if (isNumberNaN(ctrl.$modelValue)) {
1628-
return;
1629-
}
1630-
1631-
if (supportsRange) {
1632-
var elVal = element.val();
1633-
// IE11 doesn't set the el val correctly if the minVal is greater than the element value
1634-
if (minVal > elVal) {
1635-
elVal = minVal;
1636-
element.val(elVal);
1637-
}
1638-
ctrl.$setViewValue(elVal);
1639-
} else {
1640-
// TODO(matsko): implement validateLater to reduce number of validations
1641-
ctrl.$validate();
1642-
}
1643-
}
1644-
1645-
function maxChange(val) {
1646-
if (isDefined(val) && !isNumber(val)) {
1647-
val = parseFloat(val);
1648-
}
1649-
maxVal = isNumber(val) && !isNaN(val) ? val : undefined;
1650-
// ignore changes before model is initialized
1651-
if (isNumberNaN(ctrl.$modelValue)) {
1652-
return;
1653-
}
1654-
1655-
if (supportsRange) {
1656-
var elVal = element.val();
1657-
// IE11 doesn't set the el val correctly if the maxVal is less than the element value
1658-
if (maxVal < elVal) {
1659-
element.val(maxVal);
1660-
// IE11 and Chrome don't set the value to the minVal when max < min
1661-
elVal = maxVal < minVal ? minVal : maxVal;
1662-
}
1663-
ctrl.$setViewValue(elVal);
1664-
} else {
1665-
// TODO(matsko): implement validateLater to reduce number of validations
1666-
ctrl.$validate();
1667-
}
1668-
}
1669-
1670-
}
1671-
16721435
function urlInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16731436
// Note: no badInputChecker here by purpose as `url` is only a validation
16741437
// in browsers, i.e. we can always read out input.value even if it is not valid!

src/ng/directive/ngModel.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,8 +883,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
883883
ctrl.$viewValue = ctrl.$$lastCommittedViewValue = viewValue;
884884
ctrl.$render();
885885

886-
// It is possible that model and view value have been updated during render
887-
ctrl.$$runValidators(ctrl.$modelValue, ctrl.$viewValue, noop);
886+
ctrl.$$runValidators(modelValue, viewValue, noop);
888887
}
889888
}
890889

0 commit comments

Comments
 (0)