@@ -1550,7 +1550,9 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1550
1550
var minVal = 0 ,
1551
1551
maxVal = 100 ,
1552
1552
supportsRange = ctrl . $$hasNativeValidators && element [ 0 ] . type === 'range' ,
1553
- validity = element [ 0 ] . validity ;
1553
+ validity = element [ 0 ] . validity ,
1554
+ minAttrType = isDefined ( attr . ngMin ) ? 'ngMin' : isDefined ( attr . min ) ? 'min' : false ,
1555
+ maxAttrType = isDefined ( attr . ngMax ) ? 'ngMax' : isDefined ( attr . max ) ? 'max' : false ;
1554
1556
1555
1557
var originalRender = ctrl . $render ;
1556
1558
@@ -1563,11 +1565,54 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1563
1565
} :
1564
1566
originalRender ;
1565
1567
1568
+ if ( minAttrType ) {
1569
+ ctrl . $validators . min = minAttrType === 'min' && supportsRange ?
1570
+ function noopMinValidator ( ) {
1571
+ // Since all browsers set the input to a valid value, we don't need to check validity
1572
+ return true ;
1573
+ } :
1574
+ // ngMin doesn't set the min attr, so the browser doesn't adjust the input value as setting min would
1575
+ function minValidator ( modelValue , viewValue ) {
1576
+ return ctrl . $isEmpty ( viewValue ) || isUndefined ( minVal ) || viewValue >= minVal ;
1577
+ } ;
1578
+
1579
+ setInitialValueAndObserver ( minAttrType , 'min' , minChange ) ;
1580
+ }
1581
+
1582
+ if ( maxAttrType ) {
1583
+ ctrl . $validators . max = maxAttrType === 'max' && supportsRange ?
1584
+ function noopMaxValidator ( ) {
1585
+ // Since all browsers set the input to a valid value, we don't need to check validity
1586
+ return true ;
1587
+ } :
1588
+ // ngMax doesn't set the max attr, so the browser doesn't adjust the input value as setting max would
1589
+ function maxValidator ( modelValue , viewValue ) {
1590
+ return ctrl . $isEmpty ( viewValue ) || isUndefined ( maxVal ) || viewValue <= maxVal ;
1591
+ } ;
1592
+
1593
+ setInitialValueAndObserver ( maxAttrType , 'max' , maxChange ) ;
1594
+ }
1595
+
1596
+ function setInitialValueAndObserver ( attrType , attrName , changeFn ) {
1597
+ var initialAttrValue ;
1598
+ if ( attrType === attrName ) {
1599
+ initialAttrValue = attr [ attrName ] ;
1600
+ // Set the actual element attribute so that the browser can adjust the value based on
1601
+ // the max value, which might be interpolated
1602
+ element . attr ( attrName , initialAttrValue ) ;
1603
+ }
1604
+
1605
+ // This initalizes the min/max value, so that non-support browsers validate with the correct
1606
+ // values during the initial $render
1607
+ changeFn ( initialAttrValue ) ;
1608
+ attr . $observe ( attrName , changeFn ) ;
1609
+ }
1610
+
1566
1611
function minChange ( val ) {
1567
1612
if ( isDefined ( val ) && ! isNumber ( val ) ) {
1568
1613
val = parseFloat ( val ) ;
1569
1614
}
1570
- minVal = isNumber ( val ) && ! isNaN ( val ) ? val : undefined ;
1615
+ minVal = isNumber ( val ) && ! isNaN ( val ) ? val : 0 ;
1571
1616
// ignore changes before model is initialized
1572
1617
if ( isNumber ( ctrl . $modelValue ) && isNaN ( ctrl . $modelValue ) ) {
1573
1618
return ;
@@ -1587,36 +1632,19 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1587
1632
}
1588
1633
}
1589
1634
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
1635
function maxChange ( val ) {
1608
1636
if ( isDefined ( val ) && ! isNumber ( val ) ) {
1609
1637
val = parseFloat ( val ) ;
1610
1638
}
1611
- maxVal = isNumber ( val ) && ! isNaN ( val ) ? val : undefined ;
1639
+ maxVal = isNumber ( val ) && ! isNaN ( val ) ? val : 100 ;
1612
1640
// ignore changes before model is initialized
1613
1641
if ( isNumber ( ctrl . $modelValue ) && isNaN ( ctrl . $modelValue ) ) {
1614
1642
return ;
1615
1643
}
1616
1644
1617
1645
if ( supportsRange && maxAttrType === 'max' ) {
1618
1646
var elVal = element . val ( ) ;
1619
- // IE11 doesn't set the el val correctly if the maxVal is less than the element value
1647
+ // IE11 doesn't set the el val correctly if the maxVal is greater than the element value
1620
1648
if ( maxVal < elVal ) {
1621
1649
element . val ( maxVal ) ;
1622
1650
elVal = minVal ;
@@ -1627,22 +1655,6 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1627
1655
ctrl . $validate ( ) ;
1628
1656
}
1629
1657
}
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
1658
1647
1659
}
1648
1660
0 commit comments