MCPcopy Create free account
hub / github.com/angular/angular / setupCustomControl

Function setupCustomControl

packages/forms/src/directives/ng_control.ts:187–239  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

185 }
186
187 protected setupCustomControl(): void {
188 this.subscription?.unsubscribe();
189
190 const control = this.control;
191 const cdr = this.injector?.get(ChangeDetectorRef);
192 if (!control || !cdr) {
193 return;
194 }
195
196 // Merge the validators collected from DI (validator directives such as `required` or
197 // `minlength` on the element, and any `NG_VALIDATORS` provider) into the control, as
198 // `setUpControlValueAccessor` does. When FormControlDirective or FormControlName lets go of the
199 // control, `cleanUpControl` removes them again.
200 setUpValidators(control, this);
201
202 const markForCheck = cdr.markForCheck.bind(cdr);
203 this.subscription = new Subscription();
204
205 this.subscription.add(() => this.discardPendingCustomValue(control));
206
207 // `registerOnChange` and `_unregisterOnChange` are declared on `FormControl` (and
208 // `SignalFormControl`), not on `AbstractControl`. We guard `registerOnChange` at runtime.
209 if (typeof (control as FormControl).registerOnChange === 'function') {
210 const onControlChange = (newValue?: unknown, emitModelEvent?: boolean) => {
211 this.discardPendingCustomValue(control);
212 if (emitModelEvent) {
213 this.viewToModelUpdate(newValue);
214 }
215 markForCheck();
216 };
217 (control as FormControl).registerOnChange(onControlChange);
218 this.subscription.add(() => (control as FormControl)._unregisterOnChange(onControlChange));
219 }
220
221 this.subscription.add(control.valueChanges.subscribe(markForCheck));
222 this.subscription.add(control.statusChanges.subscribe(markForCheck));
223
224 this.resetSubscription?.unsubscribe();
225 this.resetSubscription = undefined;
226 if (this.userOnReset) {
227 this.resetSubscription = control.events.subscribe((event) => {
228 if (event instanceof FormResetEvent && this.control) {
229 this.userOnReset?.(this.control.value);
230 }
231 });
232 this.subscription.add(this.resetSubscription);
233 }
234
235 // Add parseErrors validator if present
236 if (this.parseErrorsValidator) {
237 control.addValidators(this.parseErrorsValidator);
238 }
239 }
240
241 /**
242 * Internal control directive creation lifecycle hook.

Callers

nothing calls this directly

Calls 9

setUpValidatorsFunction · 0.90
addValidatorsMethod · 0.80
unsubscribeMethod · 0.65
getMethod · 0.65
bindMethod · 0.65
addMethod · 0.65
registerOnChangeMethod · 0.65
_unregisterOnChangeMethod · 0.65
subscribeMethod · 0.65

Tested by

no test coverage detected