MCPcopy
hub / github.com/angular/components / _initializeControl

Method _initializeControl

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

Initializes the registered form field control.

(previousControl: MatFormFieldControl<unknown> | null)

Source from the content-addressed store, hash-verified

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

Callers 1

ngAfterContentCheckedMethod · 0.95

Calls 5

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

Tested by

no test coverage detected