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

Commit 5ac7dae

Browse files
authored
fix(input[radio]): use strict comparison when evaluating checked-ness
Closes #15283 Closes #15288 BREAKING CHANGE: When using input[radio], the checked status is now determined by doing a strict comparison between the value of the input and the ngModel.$viewValue. Previously, this was a non-strict comparison (==). This means in the following examples the radio is no longer checked: ``` <!-- this.selected = 0 --> <input type="radio" ng-model="$ctrl.selected" value="0" > <!-- this.selected = 0; this.value = false; --> <input type="radio" ng-model="$ctrl.selected" ng-value="$ctrl.value" > ``` The migration strategy is to convert values that matched with non-strict conversion so that they will match with strict conversion.
1 parent b5a5623 commit 5ac7dae

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/ng/directive/input.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1811,9 +1811,7 @@ function radioInputType(scope, element, attr, ctrl) {
18111811
if (doTrim) {
18121812
value = trim(value);
18131813
}
1814-
// Strict comparison would cause a BC
1815-
// eslint-disable-next-line eqeqeq
1816-
element[0].checked = (value == ctrl.$viewValue);
1814+
element[0].checked = (value === ctrl.$viewValue);
18171815
};
18181816

18191817
attr.$observe('value', ctrl.$render);

test/ng/directive/inputSpec.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -3965,17 +3965,15 @@ describe('input', function() {
39653965
expect($rootScope.color).toBe('blue');
39663966
});
39673967

3968-
3969-
// We generally use strict comparison. This tests behavior we cannot change without a BC
3970-
it('should use non-strict comparison the evaluate checked-ness', function() {
3968+
it('should treat the value as a string when evaluating checked-ness', function() {
39713969
var inputElm = helper.compileInput(
39723970
'<input type="radio" ng-model="model" value="0" />');
39733971

39743972
$rootScope.$apply('model = \'0\'');
39753973
expect(inputElm[0].checked).toBe(true);
39763974

39773975
$rootScope.$apply('model = 0');
3978-
expect(inputElm[0].checked).toBe(true);
3976+
expect(inputElm[0].checked).toBe(false);
39793977
});
39803978

39813979

@@ -4229,6 +4227,18 @@ describe('input', function() {
42294227
});
42304228

42314229

4230+
it('should use strict comparison between model and value', function() {
4231+
$rootScope.selected = false;
4232+
var inputElm = helper.compileInput('<input type="radio" ng-model="selected" ng-value="false">' +
4233+
'<input type="radio" ng-model="selected" ng-value="\'\'">' +
4234+
'<input type="radio" ng-model="selected" ng-value="0">');
4235+
4236+
expect(inputElm[0].checked).toBe(true);
4237+
expect(inputElm[1].checked).toBe(false);
4238+
expect(inputElm[2].checked).toBe(false);
4239+
});
4240+
4241+
42324242
it('should watch the expression', function() {
42334243
var inputElm = helper.compileInput('<input type="radio" ng-model="selected" ng-value="value">');
42344244

0 commit comments

Comments
 (0)