Skip to content

Commit 0526689

Browse files
crisbetokara
authored andcommitted
fix(select): error if triggerValue is accessed from an empty select (#6575)
1 parent ea143a0 commit 0526689

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/lib/select/select.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,13 @@ describe('MdSelect', () => {
21842184
expect(() => fixture.detectChanges()).not.toThrow();
21852185
}));
21862186

2187+
2188+
it('should not throw when the triggerValue is accessed when there is no selected value', () => {
2189+
const fixture = TestBed.createComponent(BasicSelect);
2190+
fixture.detectChanges();
2191+
2192+
expect(() => fixture.componentInstance.select.triggerValue).not.toThrow();
2193+
});
21872194
});
21882195

21892196
describe('change event', () => {

src/lib/select/select.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,12 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
568568

569569
/** The value displayed in the trigger. */
570570
get triggerValue(): string {
571+
if (!this._selectionModel || this._selectionModel.isEmpty()) {
572+
return '';
573+
}
574+
571575
if (this._multiple) {
572-
let selectedOptions = this._selectionModel.selected.map(option => option.viewValue);
576+
const selectedOptions = this._selectionModel.selected.map(option => option.viewValue);
573577

574578
if (this._isRtl()) {
575579
selectedOptions.reverse();

0 commit comments

Comments
 (0)