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

Commit ebe2a87

Browse files
topherfangioThomasBurleson
authored andcommitted
fix(checkbox): Undefined ng-checked value now shows as unchecked.
A recent change caused an undefined `ng-checked` value to style the checkbox as checked via the `md-checked` CSS class. - Add appropriate test. - Fix check to cast value to a boolean first. - Fix indentation of watch statement. Fixes #9280. Closes #9281
1 parent f5bb5b0 commit ebe2a87

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/components/checkbox/checkbox.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $
110110

111111
if (attr.ngChecked) {
112112
scope.$watch(
113-
scope.$eval.bind(scope, attr.ngChecked),
114-
ngModelCtrl.$setViewValue.bind(ngModelCtrl)
113+
scope.$eval.bind(scope, attr.ngChecked),
114+
ngModelCtrl.$setViewValue.bind(ngModelCtrl)
115115
);
116116
}
117117

@@ -186,7 +186,8 @@ function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $
186186
}
187187

188188
function render() {
189-
element.toggleClass('md-checked', ngModelCtrl.$viewValue && !isIndeterminate);
189+
// Cast the $viewValue to a boolean since it could be undefined
190+
element.toggleClass('md-checked', !!ngModelCtrl.$viewValue && !isIndeterminate);
190191
}
191192

192193
function setIndeterminateState(newValue) {
@@ -196,6 +197,6 @@ function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $
196197
}
197198
element.toggleClass('md-indeterminate', isIndeterminate);
198199
}
199-
};
200+
}
200201
}
201202
}

src/components/checkbox/checkbox.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ describe('mdCheckbox', function() {
248248
expect(checkbox.hasClass('ng-invalid')).toBe(true);
249249
});
250250

251+
it('properly unsets the md-checked CSS if ng-checked is undefined', function() {
252+
var checkbox = compileAndLink('<md-checkbox ng-checked="value"></md-checkbox>');
253+
254+
expect(checkbox.hasClass(CHECKED_CSS)).toBe(false);
255+
});
256+
251257
describe('with the md-indeterminate attribute', function() {
252258

253259
it('should set md-indeterminate attr to true by default', function() {

0 commit comments

Comments
 (0)