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

Function ngControlCreate

packages/forms/src/directives/ng_control.ts:250–310  ·  view source on GitHub ↗

* Internal control directive creation lifecycle hook. * * The presence of this method tells the compiler to install `ɵɵControlFeature`, which will * cause this directive to be recognized as a control directive by the `ɵcontrolCreate` and * `ɵcontrol` instructions. * * @internal

(host: ControlDirectiveHost)

Source from the content-addressed store, hash-verified

248 * @internal
249 */
250 protected ngControlCreate(host: ControlDirectiveHost): void {
251 const hasNgNoCva = host.nativeElement.hasAttribute?.('ngNoCva');
252 const hasCva =
253 !hasNgNoCva &&
254 ((this.rawValueAccessors && this.rawValueAccessors.length > 0) ||
255 this.valueAccessor !== null);
256
257 if (hasCva || !host.customControl) {
258 // This control is using the CVA pattern, so ngControlCreate is a noop.
259 return;
260 }
261
262 this.isCustomControlBased = true;
263
264 // Listen to custom control value changes -> update FormControl
265 // Note: We access this.control dynamically because it may not be set yet
266 // (e.g., FormControlDirective's form input hasn't been bound)
267 host.listenToCustomControlModel((value) => {
268 const control = this.control;
269 if (!control) {
270 return;
271 }
272
273 // `_pendingValue` and `_pendingChange` are only declared on `FormControl`; cast rather than
274 // checking `instanceof FormControl` so `SignalFormControl` and custom `AbstractControl`
275 // subclasses are supported.
276 (control as FormControl)._pendingValue = value;
277 (control as FormControl)._pendingChange = true;
278 control._pendingDirty = true;
279 this.pendingCustomValue = value;
280 this.hasPendingCustomValue = true;
281
282 if (control.updateOn === 'change') {
283 this.updateControlFromCustom(control);
284 }
285 });
286
287 // Listen to touched changes from FVC
288 host.listenToCustomControlOutput('touch', () => {
289 const control = this.control;
290 if (!control) {
291 return;
292 }
293
294 control._pendingTouched = true;
295
296 // `_pendingChange` is only declared on `FormControl` (same as in `listenToCustomControlModel`).
297 if (control.updateOn === 'blur' && (control as FormControl)._pendingChange) {
298 this.updateControlFromCustom(control);
299 }
300
301 if (control.updateOn !== 'submit') {
302 control.markAsTouched();
303 }
304 });
305
306 this.customControlBindings = {};
307 this.isNativeFormElement = isNativeFormElement(host.nativeElement);

Callers

nothing calls this directly

Calls 6

isNativeFormElementFunction · 0.90
hasAttributeMethod · 0.65
markAsTouchedMethod · 0.65
findMethod · 0.45

Tested by

no test coverage detected