@@ -34,7 +34,7 @@ import {
34
34
MAT_LABEL_GLOBAL_OPTIONS ,
35
35
mixinColor ,
36
36
} from '@angular/material/core' ;
37
- import { EMPTY , fromEvent , merge } from 'rxjs' ;
37
+ import { fromEvent , merge } from 'rxjs' ;
38
38
import { startWith , take } from 'rxjs/operators' ;
39
39
import { MatError } from './error' ;
40
40
import { matFormFieldAnimations } from './form-field-animations' ;
@@ -154,14 +154,7 @@ export class MatFormField extends _MatFormFieldMixinBase
154
154
this . _appearance = value || ( this . _defaults && this . _defaults . appearance ) || 'legacy' ;
155
155
156
156
if ( this . _appearance === 'outline' && oldValue !== value ) {
157
- // @breaking -change 7.0.0 Remove this check and else block once _ngZone is required.
158
- if ( this . _ngZone ) {
159
- this . _ngZone ! . onStable . pipe ( take ( 1 ) ) . subscribe ( ( ) => {
160
- this . _ngZone ! . runOutsideAngular ( ( ) => this . updateOutlineGap ( ) ) ;
161
- } ) ;
162
- } else {
163
- Promise . resolve ( ) . then ( ( ) => this . updateOutlineGap ( ) ) ;
164
- }
157
+ this . _updateOutlineGapOnStable ( ) ;
165
158
}
166
159
}
167
160
_appearance : MatFormFieldAppearance ;
@@ -274,22 +267,30 @@ export class MatFormField extends _MatFormFieldMixinBase
274
267
275
268
ngAfterContentInit ( ) {
276
269
this . _validateControlChild ( ) ;
277
- if ( this . _control . controlType ) {
278
- this . _elementRef . nativeElement . classList
279
- . add ( `mat-form-field-type-${ this . _control . controlType } ` ) ;
270
+
271
+ const control = this . _control ;
272
+
273
+ if ( control . controlType ) {
274
+ this . _elementRef . nativeElement . classList . add ( `mat-form-field-type-${ control . controlType } ` ) ;
280
275
}
281
276
282
277
// Subscribe to changes in the child control state in order to update the form field UI.
283
- this . _control . stateChanges . pipe ( startWith < void > ( null ! ) ) . subscribe ( ( ) => {
278
+ control . stateChanges . pipe ( startWith < void > ( null ! ) ) . subscribe ( ( ) => {
284
279
this . _validatePlaceholders ( ) ;
285
280
this . _syncDescribedByIds ( ) ;
286
281
this . _changeDetectorRef . markForCheck ( ) ;
287
282
} ) ;
288
283
289
- // Run change detection if the value, prefix, or suffix changes.
290
- const valueChanges = this . _control . ngControl && this . _control . ngControl . valueChanges || EMPTY ;
291
- merge ( valueChanges , this . _prefixChildren . changes , this . _suffixChildren . changes )
292
- . subscribe ( ( ) => this . _changeDetectorRef . markForCheck ( ) ) ;
284
+ // Run change detection if the value changes.
285
+ if ( control . ngControl && control . ngControl . valueChanges ) {
286
+ control . ngControl . valueChanges . subscribe ( ( ) => this . _changeDetectorRef . markForCheck ( ) ) ;
287
+ }
288
+
289
+ // Run change detection and update the outline if the suffix or prefix changes.
290
+ merge ( this . _prefixChildren . changes , this . _suffixChildren . changes ) . subscribe ( ( ) => {
291
+ this . _updateOutlineGapOnStable ( ) ;
292
+ this . _changeDetectorRef . markForCheck ( ) ;
293
+ } ) ;
293
294
294
295
// Re-validate when the number of hints changes.
295
296
this . _hintChildren . changes . pipe ( startWith ( null ) ) . subscribe ( ( ) => {
@@ -504,4 +505,14 @@ export class MatFormField extends _MatFormFieldMixinBase
504
505
private _getStartEnd ( rect : ClientRect ) : number {
505
506
return this . _dir && this . _dir . value === 'rtl' ? rect . right : rect . left ;
506
507
}
508
+
509
+ /** Updates the outline gap the new time the zone stabilizes. */
510
+ private _updateOutlineGapOnStable ( ) {
511
+ // @breaking -change 7.0.0 Remove this check and else block once _ngZone is required.
512
+ if ( this . _ngZone ) {
513
+ this . _ngZone . onStable . pipe ( take ( 1 ) ) . subscribe ( ( ) => this . updateOutlineGap ( ) ) ;
514
+ } else {
515
+ Promise . resolve ( ) . then ( ( ) => this . updateOutlineGap ( ) ) ;
516
+ }
517
+ }
507
518
}
0 commit comments