From 0526689fdec1a34c0aa3adf93dcbfc7ec601b07a Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Wed, 23 Aug 2017 02:43:49 +0200 Subject: [PATCH] fix(select): error if triggerValue is accessed from an empty select (#6575) --- src/lib/select/select.spec.ts | 7 +++++++ src/lib/select/select.ts | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/lib/select/select.spec.ts b/src/lib/select/select.spec.ts index 5c12c95b730..cdab73d963b 100644 --- a/src/lib/select/select.spec.ts +++ b/src/lib/select/select.spec.ts @@ -2184,6 +2184,13 @@ describe('MdSelect', () => { expect(() => fixture.detectChanges()).not.toThrow(); })); + + it('should not throw when the triggerValue is accessed when there is no selected value', () => { + const fixture = TestBed.createComponent(BasicSelect); + fixture.detectChanges(); + + expect(() => fixture.componentInstance.select.triggerValue).not.toThrow(); + }); }); describe('change event', () => { diff --git a/src/lib/select/select.ts b/src/lib/select/select.ts index 95d5d282c06..fd76f0b7805 100644 --- a/src/lib/select/select.ts +++ b/src/lib/select/select.ts @@ -568,8 +568,12 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On /** The value displayed in the trigger. */ get triggerValue(): string { + if (!this._selectionModel || this._selectionModel.isEmpty()) { + return ''; + } + if (this._multiple) { - let selectedOptions = this._selectionModel.selected.map(option => option.viewValue); + const selectedOptions = this._selectionModel.selected.map(option => option.viewValue); if (this._isRtl()) { selectedOptions.reverse();