MCPcopy Create free account
hub / github.com/angular/components / _initializeControl

Method _initializeControl

src/material/form-field/form-field.ts:470–512  ·  view source on GitHub ↗

Initializes the registered form field control.

(previousControl: MatFormFieldControl<unknown> | null)

Source from the content-addressed store, hash-verified

468
469 /** Initializes the registered form field control. */
470 private _initializeControl(previousControl: MatFormFieldControl<unknown> | null) {
471 const control = this._control;
472 const classPrefix = 'mat-mdc-form-field-type-';
473
474 if (previousControl) {
475 this._elementRef.nativeElement.classList.remove(classPrefix + previousControl.controlType);
476 }
477
478 if (control.controlType) {
479 this._elementRef.nativeElement.classList.add(classPrefix + control.controlType);
480 }
481
482 // Subscribe to changes in the child control state in order to update the form field UI.
483 this._stateChanges?.unsubscribe();
484 this._stateChanges = control.stateChanges?.subscribe(() => {
485 this._updateFocusState();
486 this._changeDetectorRef.markForCheck();
487 });
488
489 // Updating the `aria-describedby` touches the DOM. Only do it if it actually needs to change.
490 this._describedByChanges?.unsubscribe();
491 this._describedByChanges = control.stateChanges
492 ?.pipe(
493 startWith([undefined, undefined] as const),
494 map(
495 () => [this._unwrapMaybeSignal(control.errorState), control.userAriaDescribedBy] as const,
496 ),
497 pairwise(),
498 filter(([[prevErrorState, prevDescribedBy], [currentErrorState, currentDescribedBy]]) => {
499 return prevErrorState !== currentErrorState || prevDescribedBy !== currentDescribedBy;
500 }),
501 )
502 .subscribe(() => this._syncDescribedByIds());
503
504 this._valueChanges?.unsubscribe();
505
506 // Run change detection if the value changes.
507 if (control.ngControl && control.ngControl.valueChanges && !isFieldTree(control.ngField)) {
508 this._valueChanges = control.ngControl.valueChanges
509 .pipe(takeUntil(this._destroyed))
510 .subscribe(() => this._changeDetectorRef.markForCheck());
511 }
512 }
513
514 private _checkPrefixAndSuffixTypes() {
515 this._hasIconPrefix = !!this._prefixChildren.find(p => !p._isText);

Callers 1

ngAfterContentCheckedMethod · 0.95

Calls 6

_updateFocusStateMethod · 0.95
_unwrapMaybeSignalMethod · 0.95
_syncDescribedByIdsMethod · 0.95
markForCheckMethod · 0.80
removeMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected