@@ -1027,6 +1027,113 @@ var inputType = {
1027
1027
*/
1028
1028
'radio' : radioInputType ,
1029
1029
1030
+ /**
1031
+ * @ngdoc input
1032
+ * @name input[range]
1033
+ *
1034
+ * @description
1035
+ * Native range input with validation and transformation.
1036
+ *
1037
+ * The model for the range input must always be a `Number`.
1038
+ *
1039
+ * IE9 and other browsers that do not support the `range` type fall back
1040
+ * to a text input. Model binding, validation and number parsing are nevertheless supported.
1041
+ *
1042
+ * Browsers that support range (latest Chrome, Safari, Firefox, Edge) treat `input[range]`
1043
+ * in a way that never allows the input to hold an invalid value. That means:
1044
+ * - any non-numerical value is set to `(max + min) / 2`.
1045
+ * - any numerical value that is less than the current min val, or greater than the current max val
1046
+ * is set to the min / max val respectively.
1047
+ *
1048
+ * This has the following consequences for Angular:
1049
+ *
1050
+ * Since the element value should always reflect the current model value, a range input
1051
+ * will set the bound ngModel expression to the value that the browser has set for the
1052
+ * input element. For example, in the following input `<input type="range" ng-model="model.value">`,
1053
+ * if the application sets `model.value = null`, the browser will set the input to `'50'`.
1054
+ * Angular will then set the model to `50`, to prevent input and model value being out of sync.
1055
+ *
1056
+ * That means the model for range will immediately be set to `50` after `ngModel` has been
1057
+ * initialized. It also means a range input can never have the required error.
1058
+ *
1059
+ * This does not only affect changes to the model value, but also to the values of the `min` and
1060
+ * `max` attributes. When these change in a way that will cause the browser to modify the input value,
1061
+ * Angular will also update the model value.
1062
+ *
1063
+ * Automatic value adjustment also means that a range input element can never have the `required`,
1064
+ * `min`, or `max` errors, except when using `ngMax` and `ngMin`, which are not affected by automatic
1065
+ * value adjustment, because they do not set the `min` and `max` attributes.
1066
+ *
1067
+ * @param {string } ngModel Assignable angular expression to data-bind to.
1068
+ * @param {string= } name Property name of the form under which the control is published.
1069
+ * @param {string= } min Sets the `min` validation to ensure that the value entered is greater
1070
+ * than `min`. Can be interpolated.
1071
+ * @param {string= } max Sets the `max` validation to ensure that the value entered is less than `max`.
1072
+ * Can be interpolated.
1073
+ * @param {string= } ngMin Takes an expression. Sets the `min` validation to ensure that the value
1074
+ * entered is greater than `min`. Does not set the `min` attribute and therefore
1075
+ * adds no native HTML5 validation. It also means the browser won't adjust the
1076
+ * element value in case `min` is greater than the current value.
1077
+ * @param {string= } ngMax Takes an expression. Sets the `max` validation to ensure that the value
1078
+ * entered is less than `max`. Does not set the `max` attribute and therefore
1079
+ * adds no native HTML5 validation. It also means the browser won't adjust the
1080
+ * element value in case `max` is less than the current value.
1081
+ * @param {string= } ngChange Angular expression to be executed when the ngModel value changes due
1082
+ * to user interaction with the input element.
1083
+ *
1084
+ * @example
1085
+ <example name="range-input-directive" module="rangeExample">
1086
+ <file name="index.html">
1087
+ <script>
1088
+ angular.module('rangeExample', [])
1089
+ .controller('ExampleController', ['$scope', function($scope) {
1090
+ $scope.value = 75;
1091
+ $scope.min = 10;
1092
+ $scope.max = 90;
1093
+ }]);
1094
+ </script>
1095
+ <form name="myForm" ng-controller="ExampleController">
1096
+
1097
+ Model as range: <input type="range" name="range" ng-model="value" min="{{min}}" max="{{max}}">
1098
+ <hr>
1099
+ Model as number: <input type="number" ng-model="value"><br>
1100
+ Min: <input type="number" ng-model="min"><br>
1101
+ Max: <input type="number" ng-model="max"><br>
1102
+ value = <code>{{value}}</code><br/>
1103
+ myForm.range.$valid = <code>{{myForm.range.$valid}}</code><br/>
1104
+ myForm.range.$error = <code>{{myForm.range.$error}}</code>
1105
+ </form>
1106
+ </file>
1107
+ </example>
1108
+
1109
+ * ## Range Input with ngMin & ngMax attributes
1110
+
1111
+ * @example
1112
+ <example name="range-input-directive-ng" module="rangeExample">
1113
+ <file name="index.html">
1114
+ <script>
1115
+ angular.module('rangeExample', [])
1116
+ .controller('ExampleController', ['$scope', function($scope) {
1117
+ $scope.value = 75;
1118
+ $scope.min = 10;
1119
+ $scope.max = 90;
1120
+ }]);
1121
+ </script>
1122
+ <form name="myForm" ng-controller="ExampleController">
1123
+ Model as range: <input type="range" name="range" ng-model="value" ng-min="min" ng-max="max">
1124
+ <hr>
1125
+ Model as number: <input type="number" ng-model="value"><br>
1126
+ Min: <input type="number" ng-model="min"><br>
1127
+ Max: <input type="number" ng-model="max"><br>
1128
+ value = <code>{{value}}</code><br/>
1129
+ myForm.range.$valid = <code>{{myForm.range.$valid}}</code><br/>
1130
+ myForm.range.$error = <code>{{myForm.range.$error}}</code>
1131
+ </form>
1132
+ </file>
1133
+ </example>
1134
+
1135
+ */
1136
+ 'range' : rangeInputType ,
1030
1137
1031
1138
/**
1032
1139
* @ngdoc input
@@ -1378,10 +1485,7 @@ function badInputChecker(scope, element, attr, ctrl) {
1378
1485
}
1379
1486
}
1380
1487
1381
- function numberInputType ( scope , element , attr , ctrl , $sniffer , $browser ) {
1382
- badInputChecker ( scope , element , attr , ctrl ) ;
1383
- baseInputType ( scope , element , attr , ctrl , $sniffer , $browser ) ;
1384
-
1488
+ function numberFormatterParser ( ctrl ) {
1385
1489
ctrl . $$parserName = 'number' ;
1386
1490
ctrl . $parsers . push ( function ( value ) {
1387
1491
if ( ctrl . $isEmpty ( value ) ) return null ;
@@ -1398,6 +1502,12 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1398
1502
}
1399
1503
return value ;
1400
1504
} ) ;
1505
+ }
1506
+
1507
+ function numberInputType ( scope , element , attr , ctrl , $sniffer , $browser ) {
1508
+ badInputChecker ( scope , element , attr , ctrl ) ;
1509
+ numberFormatterParser ( ctrl ) ;
1510
+ baseInputType ( scope , element , attr , ctrl , $sniffer , $browser ) ;
1401
1511
1402
1512
if ( isDefined ( attr . min ) || attr . ngMin ) {
1403
1513
var minVal ;
@@ -1432,6 +1542,110 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1432
1542
}
1433
1543
}
1434
1544
1545
+ function rangeInputType ( scope , element , attr , ctrl , $sniffer , $browser ) {
1546
+ badInputChecker ( scope , element , attr , ctrl ) ;
1547
+ numberFormatterParser ( ctrl ) ;
1548
+ baseInputType ( scope , element , attr , ctrl , $sniffer , $browser ) ;
1549
+
1550
+ var minVal = 0 ,
1551
+ maxVal = 100 ,
1552
+ supportsRange = ctrl . $$hasNativeValidators && element [ 0 ] . type === 'range' ,
1553
+ validity = element [ 0 ] . validity ;
1554
+
1555
+ var originalRender = ctrl . $render ;
1556
+
1557
+ ctrl . $render = supportsRange && isDefined ( validity . rangeUnderflow ) && isDefined ( validity . rangeOverflow ) ?
1558
+ //Browsers that implement range will set these values automatically, but reading the adjusted values after
1559
+ //$render would cause the min / max validators to be applied with the wrong value
1560
+ function rangeRender ( ) {
1561
+ originalRender ( ) ;
1562
+ ctrl . $setViewValue ( element . val ( ) ) ;
1563
+ } :
1564
+ originalRender ;
1565
+
1566
+ function minChange ( val ) {
1567
+ if ( isDefined ( val ) && ! isNumber ( val ) ) {
1568
+ val = parseFloat ( val ) ;
1569
+ }
1570
+ minVal = isNumber ( val ) && ! isNaN ( val ) ? val : undefined ;
1571
+ // ignore changes before model is initialized
1572
+ if ( isNumberNaN ( ctrl . $modelValue ) ) {
1573
+ return ;
1574
+ }
1575
+
1576
+ if ( supportsRange && minAttrType === 'min' ) {
1577
+ var elVal = element . val ( ) ;
1578
+ // IE11 doesn't set the el val correctly if the minVal is greater than the element value
1579
+ if ( minVal > elVal ) {
1580
+ element . val ( minVal ) ;
1581
+ elVal = minVal ;
1582
+ }
1583
+ ctrl . $setViewValue ( elVal ) ;
1584
+ } else {
1585
+ // TODO(matsko): implement validateLater to reduce number of validations
1586
+ ctrl . $validate ( ) ;
1587
+ }
1588
+ }
1589
+
1590
+ var minAttrType = isDefined ( attr . ngMin ) ? 'ngMin' : isDefined ( attr . min ) ? 'min' : false ;
1591
+ if ( minAttrType ) {
1592
+ ctrl . $validators . min = isDefined ( attr . min ) && supportsRange ?
1593
+ function noopMinValidator ( value ) {
1594
+ // Since all browsers set the input to a valid value, we don't need to check validity
1595
+ return true ;
1596
+ } :
1597
+ // ngMin doesn't set the min attr, so the browser doesn't adjust the input value as setting min would
1598
+ function minValidator ( modelValue , viewValue ) {
1599
+ return ctrl . $isEmpty ( viewValue ) || isUndefined ( minVal ) || viewValue >= minVal ;
1600
+ } ;
1601
+
1602
+ // Assign minVal when the directive is linked. This won't run the validators as the model isn't ready yet
1603
+ minChange ( attr . min ) ;
1604
+ attr . $observe ( 'min' , minChange ) ;
1605
+ }
1606
+
1607
+ function maxChange ( val ) {
1608
+ if ( isDefined ( val ) && ! isNumber ( val ) ) {
1609
+ val = parseFloat ( val ) ;
1610
+ }
1611
+ maxVal = isNumber ( val ) && ! isNaN ( val ) ? val : undefined ;
1612
+ // ignore changes before model is initialized
1613
+ if ( isNumberNaN ( ctrl . $modelValue ) ) {
1614
+ return ;
1615
+ }
1616
+
1617
+ if ( supportsRange && maxAttrType === 'max' ) {
1618
+ var elVal = element . val ( ) ;
1619
+ // IE11 doesn't set the el val correctly if the maxVal is less than the element value
1620
+ if ( maxVal < elVal ) {
1621
+ element . val ( maxVal ) ;
1622
+ elVal = minVal ;
1623
+ }
1624
+ ctrl . $setViewValue ( elVal ) ;
1625
+ } else {
1626
+ // TODO(matsko): implement validateLater to reduce number of validations
1627
+ ctrl . $validate ( ) ;
1628
+ }
1629
+ }
1630
+ var maxAttrType = isDefined ( attr . max ) ? 'max' : attr . ngMax ? 'ngMax' : false ;
1631
+ if ( maxAttrType ) {
1632
+ ctrl . $validators . max = isDefined ( attr . max ) && supportsRange ?
1633
+ function noopMaxValidator ( ) {
1634
+ // Since all browsers set the input to a valid value, we don't need to check validity
1635
+ return true ;
1636
+ } :
1637
+ // ngMax doesn't set the max attr, so the browser doesn't adjust the input value as setting max would
1638
+ function maxValidator ( modelValue , viewValue ) {
1639
+ return ctrl . $isEmpty ( viewValue ) || isUndefined ( maxVal ) || viewValue <= maxVal ;
1640
+ } ;
1641
+
1642
+ // Assign maxVal when the directive is linked. This won't run the validators as the model isn't ready yet
1643
+ maxChange ( attr . max ) ;
1644
+ attr . $observe ( 'max' , maxChange ) ;
1645
+ }
1646
+
1647
+ }
1648
+
1435
1649
function urlInputType ( scope , element , attr , ctrl , $sniffer , $browser ) {
1436
1650
// Note: no badInputChecker here by purpose as `url` is only a validation
1437
1651
// in browsers, i.e. we can always read out input.value even if it is not valid!
0 commit comments