@@ -34,7 +34,7 @@ import {
3434 MAT_LABEL_GLOBAL_OPTIONS ,
3535 mixinColor ,
3636} from '@angular/material/core' ;
37- import { EMPTY , fromEvent , merge } from 'rxjs' ;
37+ import { fromEvent , merge } from 'rxjs' ;
3838import { startWith , take } from 'rxjs/operators' ;
3939import { MatError } from './error' ;
4040import { matFormFieldAnimations } from './form-field-animations' ;
@@ -154,14 +154,7 @@ export class MatFormField extends _MatFormFieldMixinBase
154154 this . _appearance = value || ( this . _defaults && this . _defaults . appearance ) || 'legacy' ;
155155
156156 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 ( ) ;
165158 }
166159 }
167160 _appearance : MatFormFieldAppearance ;
@@ -274,22 +267,30 @@ export class MatFormField extends _MatFormFieldMixinBase
274267
275268 ngAfterContentInit ( ) {
276269 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 } ` ) ;
280275 }
281276
282277 // 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 ( ( ) => {
284279 this . _validatePlaceholders ( ) ;
285280 this . _syncDescribedByIds ( ) ;
286281 this . _changeDetectorRef . markForCheck ( ) ;
287282 } ) ;
288283
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+ } ) ;
293294
294295 // Re-validate when the number of hints changes.
295296 this . _hintChildren . changes . pipe ( startWith ( null ) ) . subscribe ( ( ) => {
@@ -504,4 +505,14 @@ export class MatFormField extends _MatFormFieldMixinBase
504505 private _getStartEnd ( rect : ClientRect ) : number {
505506 return this . _dir && this . _dir . value === 'rtl' ? rect . right : rect . left ;
506507 }
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+ }
507518}
0 commit comments