Skip to content

Commit c30eed0

Browse files
committed
fix(input[radio]): use strict comparison when evaluating checked-ness
Closes angular#15283 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 daa47e3 commit c30eed0

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
@@ -1761,9 +1761,7 @@ function radioInputType(scope, element, attr, ctrl) {
17611761
if (doTrim) {
17621762
value = trim(value);
17631763
}
1764-
// Strict comparison would cause a BC
1765-
// eslint-disable-next-line eqeqeq
1766-
element[0].checked = (value == ctrl.$viewValue);
1764+
element[0].checked = (value === ctrl.$viewValue);
17671765
};
17681766

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

test/ng/directive/inputSpec.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -3867,17 +3867,15 @@ describe('input', function() {
38673867
expect($rootScope.color).toBe('blue');
38683868
});
38693869

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

38763874
$rootScope.$apply('model = \'0\'');
38773875
expect(inputElm[0].checked).toBe(true);
38783876

38793877
$rootScope.$apply('model = 0');
3880-
expect(inputElm[0].checked).toBe(true);
3878+
expect(inputElm[0].checked).toBe(false);
38813879
});
38823880

38833881

@@ -4131,6 +4129,18 @@ describe('input', function() {
41314129
});
41324130

41334131

4132+
it('should use strict comparison between model and value', function() {
4133+
$rootScope.selected = false;
4134+
var inputElm = helper.compileInput('<input type="radio" ng-model="selected" ng-value="false">' +
4135+
'<input type="radio" ng-model="selected" ng-value="\'\'">' +
4136+
'<input type="radio" ng-model="selected" ng-value="0">');
4137+
4138+
expect(inputElm[0].checked).toBe(true);
4139+
expect(inputElm[1].checked).toBe(false);
4140+
expect(inputElm[2].checked).toBe(false);
4141+
});
4142+
4143+
41344144
it('should watch the expression', function() {
41354145
var inputElm = helper.compileInput('<input type="radio" ng-model="selected" ng-value="value">');
41364146

0 commit comments

Comments
 (0)