Skip to content

Commit

Permalink
fix(select): exception if selected value is accessed on init (#3785)
Browse files Browse the repository at this point in the history
Fixes an error that is thrown if something attempts to access the `selected` value of `md-select` too early. The issue comes from the fact that the underlying `SelectionModel` gets initialized too late in `ngAfterContentInit`.

Fixes #3750.
  • Loading branch information
crisbeto authored and tinayuangao committed Mar 29, 2017
1 parent 4282917 commit e82457c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ describe('MdSelect', () => {
ThrowsErrorOnInit,
BasicSelectOnPush,
BasicSelectOnPushPreselected,
SelectWithPlainTabindex
SelectWithPlainTabindex,
SelectEarlyAccessSibling
],
providers: [
{provide: OverlayContainer, useFactory: () => {
Expand Down Expand Up @@ -1337,6 +1338,12 @@ describe('MdSelect', () => {
}).toThrowError(new RegExp('Oh no!', 'g'));
}));

it('should not throw when trying to access the selected value on init', async(() => {
expect(() => {
TestBed.createComponent(SelectEarlyAccessSibling).detectChanges();
}).not.toThrow();
}));

});

describe('change event', () => {
Expand Down Expand Up @@ -1941,6 +1948,15 @@ class MultiSelect {
})
class SelectWithPlainTabindex { }

@Component({
selector: 'select-early-sibling-access',
template: `
<md-select #select="mdSelect"></md-select>
<div *ngIf="select.selected"></div>
`
})
class SelectEarlyAccessSibling { }


class FakeViewportRuler {
getViewportRect() {
Expand Down
8 changes: 6 additions & 2 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ViewChild,
ChangeDetectorRef,
Attribute,
OnInit,
} from '@angular/core';
import {MdOption, MdOptionSelectionChange} from '../core/option/option';
import {ENTER, SPACE} from '../core/keyboard/keycodes';
Expand Down Expand Up @@ -119,7 +120,7 @@ export type MdSelectFloatPlaceholderType = 'always' | 'never' | 'auto';
],
exportAs: 'mdSelect',
})
export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestroy {
export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlValueAccessor {
/** Whether or not the overlay panel is open. */
private _panelOpen = false;

Expand Down Expand Up @@ -311,8 +312,11 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
this._tabIndex = parseInt(tabIndex) || 0;
}

ngAfterContentInit() {
ngOnInit() {
this._selectionModel = new SelectionModel<MdOption>(this.multiple, null, false);
}

ngAfterContentInit() {
this._initKeyManager();

this._changeSubscription = this.options.changes.startWith(null).subscribe(() => {
Expand Down

0 comments on commit e82457c

Please sign in to comment.