@@ -31,6 +31,8 @@ import {
3131 ViewChild ,
3232 ViewEncapsulation ,
3333 afterRender ,
34+ computed ,
35+ contentChild ,
3436 inject ,
3537} from '@angular/core' ;
3638import { AbstractControlDirective } from '@angular/forms' ;
@@ -194,14 +196,14 @@ export class MatFormField
194196 @ViewChild ( MatFormFieldNotchedOutline ) _notchedOutline : MatFormFieldNotchedOutline | undefined ;
195197 @ViewChild ( MatFormFieldLineRipple ) _lineRipple : MatFormFieldLineRipple | undefined ;
196198
197- @ContentChild ( MatLabel ) _labelChildNonStatic : MatLabel | undefined ;
198- @ContentChild ( MatLabel , { static : true } ) _labelChildStatic : MatLabel | undefined ;
199199 @ContentChild ( _MatFormFieldControl ) _formFieldControl : MatFormFieldControl < any > ;
200200 @ContentChildren ( MAT_PREFIX , { descendants : true } ) _prefixChildren : QueryList < MatPrefix > ;
201201 @ContentChildren ( MAT_SUFFIX , { descendants : true } ) _suffixChildren : QueryList < MatSuffix > ;
202202 @ContentChildren ( MAT_ERROR , { descendants : true } ) _errorChildren : QueryList < MatError > ;
203203 @ContentChildren ( MatHint , { descendants : true } ) _hintChildren : QueryList < MatHint > ;
204204
205+ private readonly _labelChild = contentChild ( MatLabel ) ;
206+
205207 /** Whether the required marker should be hidden. */
206208 @Input ( )
207209 get hideRequiredMarker ( ) : boolean {
@@ -379,9 +381,7 @@ export class MatFormField
379381 /**
380382 * Gets the id of the label element. If no label is present, returns `null`.
381383 */
382- getLabelId ( ) : string | null {
383- return this . _hasFloatingLabel ( ) ? this . _labelId : null ;
384- }
384+ getLabelId = computed ( ( ) => ( this . _hasFloatingLabel ( ) ? this . _labelId : null ) ) ;
385385
386386 /**
387387 * Gets an ElementRef for the element that a overlay attached to the form field
@@ -551,9 +551,7 @@ export class MatFormField
551551 return ! this . _platform . isBrowser && this . _prefixChildren . length && ! this . _shouldLabelFloat ( ) ;
552552 }
553553
554- _hasFloatingLabel ( ) {
555- return ! ! this . _labelChildNonStatic || ! ! this . _labelChildStatic ;
556- }
554+ _hasFloatingLabel = computed ( ( ) => ! ! this . _labelChild ( ) ) ;
557555
558556 _shouldLabelFloat ( ) {
559557 return this . _control . shouldLabelFloat || this . _shouldAlwaysFloat ( ) ;
0 commit comments