@@ -141,6 +141,7 @@ export const MAT_FORM_FIELD_DEFAULT_OPTIONS =
141
141
export class MatFormField extends _MatFormFieldMixinBase
142
142
implements AfterContentInit , AfterContentChecked , AfterViewInit , CanColor {
143
143
private _labelOptions : LabelOptions ;
144
+ private _outlineGapCalculationNeeded = false ;
144
145
145
146
/** The form-field appearance style. */
146
147
@Input ( )
@@ -300,6 +301,9 @@ export class MatFormField extends _MatFormFieldMixinBase
300
301
301
302
ngAfterContentChecked ( ) {
302
303
this . _validateControlChild ( ) ;
304
+ if ( this . _outlineGapCalculationNeeded ) {
305
+ this . updateOutlineGap ( ) ;
306
+ }
303
307
}
304
308
305
309
ngAfterViewInit ( ) {
@@ -448,6 +452,13 @@ export class MatFormField extends _MatFormFieldMixinBase
448
452
return ;
449
453
}
450
454
455
+ // If the element is not present in the DOM, the outline gap will need to be calculated
456
+ // the next time it is checked and in the DOM.
457
+ if ( document && ! document . documentElement . contains ( this . _elementRef . nativeElement ) ) {
458
+ this . _outlineGapCalculationNeeded = true ;
459
+ return ;
460
+ }
461
+
451
462
let startWidth = 0 ;
452
463
let gapWidth = 0 ;
453
464
const startEls = this . _connectionContainerRef . nativeElement . querySelectorAll < HTMLElement > (
@@ -459,9 +470,6 @@ export class MatFormField extends _MatFormFieldMixinBase
459
470
// getBoundingClientRect isn't available on the server.
460
471
return ;
461
472
}
462
- if ( ! document . documentElement . contains ( this . _elementRef . nativeElement ) ) {
463
- return ;
464
- }
465
473
466
474
const containerStart = this . _getStartEnd (
467
475
this . _connectionContainerRef . nativeElement . getBoundingClientRect ( ) ) ;
@@ -481,6 +489,8 @@ export class MatFormField extends _MatFormFieldMixinBase
481
489
for ( let i = 0 ; i < gapEls . length ; i ++ ) {
482
490
gapEls . item ( i ) . style . width = `${ gapWidth } px` ;
483
491
}
492
+
493
+ this . _outlineGapCalculationNeeded = false ;
484
494
}
485
495
486
496
/** Gets the start end of the rect considering the current directionality. */
0 commit comments