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

Commit 1dfc7cb

Browse files
committed
remove support for ngMin / ngMax
1 parent b28bfdb commit 1dfc7cb

File tree

2 files changed

+21
-114
lines changed

2 files changed

+21
-114
lines changed

src/ng/directive/input.js

+21-29
Original file line numberDiff line numberDiff line change
@@ -1061,23 +1061,18 @@ var inputType = {
10611061
* Angular will also update the model value.
10621062
*
10631063
* 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.
1064+
* `min`, or `max` errors.
1065+
*
1066+
* Note that `input[range]` is not compatible with`ngMax` and `ngMin`, because they do not set the
1067+
* `min` and `max` attributes, which means that the browser won't automatically adjust the input
1068+
* value based on their values, and will always assume min = 0 and max = 100.
10661069
*
10671070
* @param {string} ngModel Assignable angular expression to data-bind to.
10681071
* @param {string=} name Property name of the form under which the control is published.
10691072
* @param {string=} min Sets the `min` validation to ensure that the value entered is greater
10701073
* than `min`. Can be interpolated.
10711074
* @param {string=} max Sets the `max` validation to ensure that the value entered is less than `max`.
10721075
* 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.
10811076
* @param {string=} ngChange Angular expression to be executed when the ngModel value changes due
10821077
* to user interaction with the input element.
10831078
*
@@ -1551,8 +1546,8 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15511546
minVal = supportsRange ? 0 : undefined,
15521547
maxVal = supportsRange ? 100 : undefined,
15531548
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;
1549+
hasMinAttr = isDefined(attr.min),
1550+
hasMaxAttr = isDefined(attr.max);
15561551

15571552
var originalRender = ctrl.$render;
15581553

@@ -1565,38 +1560,35 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15651560
} :
15661561
originalRender;
15671562

1568-
if (minAttrType) {
1569-
ctrl.$validators.min = minAttrType === 'min' && supportsRange ?
1563+
if (hasMinAttr) {
1564+
ctrl.$validators.min = supportsRange ?
15701565
// Since all browsers set the input to a valid value, we don't need to check validity
15711566
function noopMinValidator() { return true; } :
1572-
// ngMin doesn't set the min attr, so the browser doesn't adjust the input value as setting min would
1567+
// non-support browsers validate the range
15731568
function minValidator(modelValue, viewValue) {
15741569
return ctrl.$isEmpty(viewValue) || isUndefined(minVal) || viewValue >= minVal;
15751570
};
15761571

1577-
setInitialValueAndObserver(minAttrType, 'min', minChange);
1572+
setInitialValueAndObserver('min', minChange);
15781573
}
15791574

1580-
if (maxAttrType) {
1581-
ctrl.$validators.max = maxAttrType === 'max' && supportsRange ?
1575+
if (hasMaxAttr) {
1576+
ctrl.$validators.max = supportsRange ?
15821577
// Since all browsers set the input to a valid value, we don't need to check validity
15831578
function noopMaxValidator() { return true; } :
15841579
// ngMax doesn't set the max attr, so the browser doesn't adjust the input value as setting max would
15851580
function maxValidator(modelValue, viewValue) {
15861581
return ctrl.$isEmpty(viewValue) || isUndefined(maxVal) || viewValue <= maxVal;
15871582
};
15881583

1589-
setInitialValueAndObserver(maxAttrType, 'max', maxChange);
1584+
setInitialValueAndObserver('max', maxChange);
15901585
}
15911586

1592-
function setInitialValueAndObserver(actualAttrName, htmlAttrName, changeFn) {
1593-
// e.g. max === max
1594-
if (actualAttrName === htmlAttrName) {
1595-
// interpolated attributes set the attribute value only after a digest, but we need the
1596-
// attribute value when the input is first rendered, so that the browser can adjust the
1597-
// input value based on the min/max value
1598-
element.attr(htmlAttrName, attr[htmlAttrName]);
1599-
}
1587+
function setInitialValueAndObserver(htmlAttrName, changeFn) {
1588+
// interpolated attributes set the attribute value only after a digest, but we need the
1589+
// attribute value when the input is first rendered, so that the browser can adjust the
1590+
// input value based on the min/max value
1591+
element.attr(htmlAttrName, attr[htmlAttrName]);
16001592

16011593
attr.$observe(htmlAttrName, changeFn);
16021594
}
@@ -1611,7 +1603,7 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16111603
return;
16121604
}
16131605

1614-
if (supportsRange && minAttrType === 'min') {
1606+
if (supportsRange) {
16151607
var elVal = element.val();
16161608
// IE11 doesn't set the el val correctly if the minVal is greater than the element value
16171609
if (minVal > elVal) {
@@ -1635,7 +1627,7 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16351627
return;
16361628
}
16371629

1638-
if (supportsRange && maxAttrType === 'max') {
1630+
if (supportsRange) {
16391631
var elVal = element.val();
16401632
// IE11 doesn't set the el val correctly if the maxVal is less than the element value
16411633
if (maxVal < elVal) {

test/ng/directive/inputSpec.js

-85
Original file line numberDiff line numberDiff line change
@@ -3105,49 +3105,6 @@ describe('input', function() {
31053105
}
31063106
});
31073107

3108-
describe('ngMin', function() {
3109-
3110-
it('should validate', function() {
3111-
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" ng-min="50" />');
3112-
3113-
helper.changeInputValueTo('1');
3114-
expect(inputElm).toBeInvalid();
3115-
expect(scope.value).toBeFalsy();
3116-
expect(scope.form.alias.$error.min).toBeTruthy();
3117-
3118-
helper.changeInputValueTo('100');
3119-
expect(inputElm).toBeValid();
3120-
expect(scope.value).toBe(100);
3121-
expect(scope.form.alias.$error.min).toBeFalsy();
3122-
});
3123-
3124-
it('should validate even if the ngMin value changes on-the-fly', function() {
3125-
scope.min = 10;
3126-
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" ng-min="min" />');
3127-
3128-
helper.changeInputValueTo('15');
3129-
expect(inputElm).toBeValid();
3130-
3131-
scope.min = 20;
3132-
scope.$digest();
3133-
expect(inputElm).toBeInvalid();
3134-
expect(inputElm.val()).toBe('15');
3135-
3136-
scope.min = null;
3137-
scope.$digest();
3138-
expect(inputElm).toBeValid();
3139-
3140-
scope.min = '20';
3141-
scope.$digest();
3142-
expect(inputElm).toBeInvalid();
3143-
3144-
scope.min = 'abc';
3145-
scope.$digest();
3146-
expect(inputElm).toBeValid();
3147-
});
3148-
});
3149-
3150-
31513108
describe('max', function() {
31523109

31533110
if (supportsRange) {
@@ -3303,48 +3260,6 @@ describe('input', function() {
33033260
}
33043261
});
33053262

3306-
describe('ngMax', function() {
3307-
3308-
it('should validate', function() {
3309-
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" ng-max="5" />');
3310-
3311-
helper.changeInputValueTo('20');
3312-
expect(inputElm).toBeInvalid();
3313-
expect(scope.value).toBeUndefined();
3314-
expect(scope.form.alias.$error.max).toBeTruthy();
3315-
3316-
helper.changeInputValueTo('0');
3317-
expect(inputElm).toBeValid();
3318-
expect(scope.value).toBe(0);
3319-
expect(scope.form.alias.$error.max).toBeFalsy();
3320-
});
3321-
3322-
it('should validate even if the ngMax value changes on-the-fly', function() {
3323-
scope.max = 10;
3324-
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" ng-max="max" />');
3325-
3326-
helper.changeInputValueTo('5');
3327-
expect(inputElm).toBeValid();
3328-
3329-
scope.max = 0;
3330-
scope.$digest();
3331-
expect(inputElm).toBeInvalid();
3332-
3333-
scope.max = null;
3334-
scope.$digest();
3335-
expect(inputElm).toBeValid();
3336-
3337-
scope.max = '4';
3338-
scope.$digest();
3339-
expect(inputElm).toBeInvalid();
3340-
3341-
scope.max = 'abc';
3342-
scope.$digest();
3343-
expect(inputElm).toBeValid();
3344-
});
3345-
3346-
});
3347-
33483263
if (supportsRange) {
33493264

33503265
describe('min and max', function() {

0 commit comments

Comments
 (0)