Skip to content

Commit 3fd3117

Browse files
authored
fix(checkbox): Fix native checked is not true when MdCheckbox initial checked value is true (#2055)
* Fix checked is not true when MdCheckbox initial checked value is true * Add test * fix lint
1 parent b07aa7e commit 3fd3117

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/lib/checkbox/checkbox.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ describe('MdCheckbox', () => {
100100
expect(inputElement.indeterminate).toBe(false);
101101
});
102102

103+
it('should change native element checked when check programmatically', () => {
104+
expect(inputElement.checked).toBe(false);
105+
106+
checkboxInstance.checked = true;
107+
fixture.detectChanges();
108+
109+
expect(inputElement.checked).toBe(true);
110+
});
111+
103112
it('should toggle checked state on click', () => {
104113
expect(checkboxInstance.checked).toBe(false);
105114

src/lib/checkbox/checkbox.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ChangeDetectorRef,
23
ChangeDetectionStrategy,
34
Component,
45
ElementRef,
@@ -156,7 +157,9 @@ export class MdCheckbox implements ControlValueAccessor {
156157

157158
hasFocus: boolean = false;
158159

159-
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {
160+
constructor(private _renderer: Renderer,
161+
private _elementRef: ElementRef,
162+
private _changeDetectorRef: ChangeDetectorRef) {
160163
this.color = 'accent';
161164
}
162165

@@ -174,6 +177,7 @@ export class MdCheckbox implements ControlValueAccessor {
174177
this._checked = checked;
175178
this._transitionCheckState(
176179
this._checked ? TransitionCheckState.Checked : TransitionCheckState.Unchecked);
180+
this._changeDetectorRef.markForCheck();
177181
}
178182
}
179183

0 commit comments

Comments
 (0)