@@ -1485,7 +1485,10 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1485
1485
badInputChecker ( scope , element , attr , ctrl ) ;
1486
1486
baseInputType ( scope , element , attr , ctrl , $sniffer , $browser ) ;
1487
1487
1488
- var minVal = 0 , maxVal = 100 ;
1488
+ var minVal = 0 ,
1489
+ maxVal = 100 ,
1490
+ hasNativeValidation = ctrl . $$hasNativeValidators && element [ 0 ] . validity ,
1491
+ validity = element [ 0 ] . validity ;
1489
1492
1490
1493
ctrl . $$parserName = 'number' ;
1491
1494
ctrl . $parsers . push ( function ( value ) {
@@ -1514,10 +1517,15 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1514
1517
}
1515
1518
1516
1519
if ( isDefined ( attr . min ) || attr . ngMin ) {
1517
- ctrl . $validators . min = function ( value ) {
1518
- return ctrl . $isEmpty ( value ) || isUndefined ( minVal ) || value >= minVal ;
1519
- } ;
1520
+ ctrl . $validators . min = hasNativeValidation && isDefined ( validity . rangeUnderflow ) ?
1521
+ function nativeMaxValidator ( value ) {
1522
+ return ! element [ 0 ] . validity . rangeOverflow ;
1523
+ } :
1524
+ function maxValidator ( modelValue , viewValue ) {
1525
+ return ctrl . $isEmpty ( viewValue ) || isUndefined ( maxVal ) || viewValue >= minVal ;
1526
+ } ;
1520
1527
1528
+ // Assign minVal when the directive is linked. This won't $validate as the model isn't ready yet
1521
1529
minChange ( attr . min ) ;
1522
1530
attr . $observe ( 'min' , minChange ) ;
1523
1531
}
@@ -1532,40 +1540,46 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1532
1540
}
1533
1541
1534
1542
if ( isDefined ( attr . max ) || attr . ngMax ) {
1535
- ctrl . $validators . max = ctrl . $$hasNativeValidators && isObject ( element [ 0 ] . validity ) ?
1536
- function ( value ) {
1543
+ ctrl . $validators . max = hasNativeValidation && isDefined ( validity . rangeOverflow ) ?
1544
+ function nativeMaxValidator ( value ) {
1537
1545
return ! element [ 0 ] . validity . rangeOverflow ;
1538
1546
} :
1539
- function ( modelValue , viewValue ) {
1547
+ function maxValidator ( modelValue , viewValue ) {
1540
1548
return ctrl . $isEmpty ( viewValue ) || isUndefined ( maxVal ) || viewValue <= maxVal ;
1541
1549
} ;
1542
1550
1543
1551
console . log ( 'max initial' , attr . max ) ;
1552
+ // Assign maxVal when the directive is linked. This won't $validate as the model isn't ready yet
1544
1553
maxChange ( attr . max ) ;
1545
1554
attr . $observe ( 'max' , maxChange ) ;
1546
1555
}
1547
1556
1548
1557
var originalRender = ctrl . $render ;
1549
1558
1550
- ctrl . $render = function ( ) {
1551
- if ( ctrl . $isEmpty ( ctrl . $viewValue ) ) {
1552
- // Browsers that implement range do this automatically, but IE9 does not
1553
- var viewValue = minVal + ( maxVal - minVal ) / 2 ;
1554
- ctrl . $viewValue = viewValue ;
1555
- originalRender ( ) ;
1556
- ctrl . $setViewValue ( viewValue ) ;
1557
- } else if ( ctrl . $viewValue > maxVal ) {
1558
- ctrl . $viewValue = maxVal ;
1559
- originalRender ( ) ;
1560
- ctrl . $setViewValue ( ctrl . $viewValue ) ;
1561
- } else if ( ctrl . $viewValue < minVal ) {
1562
- ctrl . $viewValue = minVal ;
1563
- originalRender ( ) ;
1564
- ctrl . $setViewValue ( ctrl . $viewValue ) ;
1565
- } else {
1566
- originalRender ( ) ;
1567
- }
1568
- } ;
1559
+ ctrl . $render = hasNativeValidation && isDefined ( validity . rangeUnderflow ) && isDefined ( validity . rangeOverflow ) ?
1560
+ //Browsers that implement range will set these values automatically, but reading the adjusted values after
1561
+ //$render would cause the min / max validators to be applied with the wrong value
1562
+ function rangeRender ( ) {
1563
+ console . log ( 'rangeRendr' ) ;
1564
+ if ( ctrl . $isEmpty ( ctrl . $viewValue ) ) {
1565
+ var viewValue = minVal + ( maxVal - minVal ) / 2 ;
1566
+ ctrl . $viewValue = viewValue ;
1567
+ originalRender ( ) ;
1568
+ ctrl . $setViewValue ( viewValue ) ;
1569
+ console . log ( 'elval' , element . val ( ) ) ;
1570
+ } else if ( ctrl . $viewValue > maxVal ) {
1571
+ ctrl . $viewValue = maxVal ;
1572
+ originalRender ( ) ;
1573
+ ctrl . $setViewValue ( ctrl . $viewValue ) ;
1574
+ } else if ( ctrl . $viewValue < minVal ) {
1575
+ ctrl . $viewValue = minVal ;
1576
+ originalRender ( ) ;
1577
+ ctrl . $setViewValue ( ctrl . $viewValue ) ;
1578
+ } else {
1579
+ originalRender ( ) ;
1580
+ }
1581
+ } :
1582
+ originalRender ;
1569
1583
1570
1584
}
1571
1585
0 commit comments