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