diff --git a/src/lib/list/selection-list.spec.ts b/src/lib/list/selection-list.spec.ts index 457d0b1532ea..0c692695d4a3 100644 --- a/src/lib/list/selection-list.spec.ts +++ b/src/lib/list/selection-list.spec.ts @@ -219,6 +219,34 @@ describe('MatSelectionList', () => { }); }); + describe('with list option selected', () => { + let fixture: ComponentFixture; + let listItemEl: DebugElement; + let selectionList: DebugElement; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [MdListModule], + declarations: [SelectionListWithSelecedOption], + }); + + TestBed.compileComponents(); + })); + + beforeEach(async(() => { + fixture = TestBed.createComponent(SelectionListWithSelecedOption); + listItemEl = fixture.debugElement.query(By.directive(MdListOption)); + selectionList = fixture.debugElement.query(By.directive(MdSelectionList)); + fixture.detectChanges(); + })); + + it('should set its initial selected state in the selectedOptions', () => { + let optionEl = listItemEl.injector.get(MdListOption); + let selectedOptions = selectionList.componentInstance.selectedOptions; + expect(selectedOptions.isSelected(optionEl)).toBeTruthy(); + }); + }); + describe('with single option', () => { let fixture: ComponentFixture; let listOption: DebugElement; @@ -456,6 +484,13 @@ class SelectionListWithDisabledOption { disableItem: boolean = false; } +@Component({template: ` + + Item + `}) +class SelectionListWithSelecedOption { +} + @Component({template: ` diff --git a/src/lib/list/selection-list.ts b/src/lib/list/selection-list.ts index 3f939fa600c7..f476fa95cd9a 100644 --- a/src/lib/list/selection-list.ts +++ b/src/lib/list/selection-list.ts @@ -23,6 +23,7 @@ import { Inject, Input, OnDestroy, + OnInit, Optional, Output, QueryList, @@ -82,8 +83,7 @@ const FOCUSED_STYLE: string = 'mat-list-item-focus'; changeDetection: ChangeDetectionStrategy.OnPush, }) export class MatListOption extends _MatListOptionMixinBase - implements AfterContentInit, OnDestroy, FocusableOption, CanDisableRipple { - + implements AfterContentInit, OnInit, OnDestroy, FocusableOption, CanDisableRipple { private _lineSetter: MatLineSetter; private _selected: boolean = false; private _disabled: boolean = false; @@ -129,6 +129,12 @@ export class MatListOption extends _MatListOptionMixinBase super(); } + ngOnInit() { + if (this.selected) { + this.selectionList.selectedOptions.select(this); + } + } + ngAfterContentInit() { this._lineSetter = new MatLineSetter(this._lines, this._renderer, this._element);