@@ -24,6 +24,8 @@ import {ControlValueAccessor, NgControl} from '@angular/forms';
24
24
import { coerceBooleanProperty } from '../core/coercion/boolean-property' ;
25
25
import { ConnectedOverlayDirective } from '../core/overlay/overlay-directives' ;
26
26
import { ViewportRuler } from '../core/overlay/position/viewport-ruler' ;
27
+ import 'rxjs/add/operator/startWith' ;
28
+
27
29
28
30
/**
29
31
* The following style constants are necessary to save here in order
@@ -123,9 +125,6 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
123
125
/** The placeholder displayed in the trigger of the select. */
124
126
private _placeholder : string ;
125
127
126
- /** Holds a value that was attempted to be assigned before the component was initialized. */
127
- private _tempValue : any ;
128
-
129
128
/** The animation state of the placeholder. */
130
129
_placeholderState = '' ;
131
130
@@ -246,15 +245,7 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
246
245
this . _initKeyManager ( ) ;
247
246
this . _resetOptions ( ) ;
248
247
249
- // Assign any values that were deferred until the component is initialized.
250
- if ( this . _tempValue ) {
251
- Promise . resolve ( null ) . then ( ( ) => {
252
- this . writeValue ( this . _tempValue ) ;
253
- this . _tempValue = null ;
254
- } ) ;
255
- }
256
-
257
- this . _changeSubscription = this . options . changes . subscribe ( ( ) => {
248
+ this . _changeSubscription = this . options . changes . startWith ( null ) . subscribe ( ( ) => {
258
249
this . _resetOptions ( ) ;
259
250
260
251
if ( this . _control ) {
@@ -267,8 +258,14 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
267
258
268
259
ngOnDestroy ( ) {
269
260
this . _dropSubscriptions ( ) ;
270
- this . _changeSubscription . unsubscribe ( ) ;
271
- this . _tabSubscription . unsubscribe ( ) ;
261
+
262
+ if ( this . _changeSubscription ) {
263
+ this . _changeSubscription . unsubscribe ( ) ;
264
+ }
265
+
266
+ if ( this . _tabSubscription ) {
267
+ this . _tabSubscription . unsubscribe ( ) ;
268
+ }
272
269
}
273
270
274
271
/** Toggles the overlay panel open or closed. */
@@ -304,11 +301,6 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
304
301
writeValue ( value : any ) : void {
305
302
if ( this . options ) {
306
303
this . _setSelectionByValue ( value ) ;
307
- } else {
308
- // In reactive forms, writeValue() will be called synchronously before
309
- // the select's child options have been created. We save the value and
310
- // assign it after everything is set up, in order to avoid lost data.
311
- this . _tempValue = value ;
312
304
}
313
305
}
314
306
0 commit comments