Skip to content

Commit 8233842

Browse files
committed
refactor(forms): Add a lightweight DI token for Control (angular#64188)
This will allow optionally injecting the Control directive without importing the full directive. PR Close angular#64188
1 parent 732635b commit 8233842

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

goldens/public-api/forms/signals/index.api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ElementRef } from '@angular/core';
1010
import { HttpResourceOptions } from '@angular/common/http';
1111
import { HttpResourceRequest } from '@angular/common/http';
1212
import * as i0 from '@angular/core';
13+
import { InjectionToken } from '@angular/core';
1314
import { Injector } from '@angular/core';
1415
import { InputSignal } from '@angular/core';
1516
import { ModelSignal } from '@angular/core';
@@ -64,6 +65,9 @@ export interface ChildFieldContext<TValue> extends RootFieldContext<TValue> {
6465
readonly key: Signal<string>;
6566
}
6667

68+
// @public
69+
export const CONTROL: InjectionToken<Control<unknown>>;
70+
6771
// @public
6872
export class Control<T> {
6973
get cva(): ControlValueAccessor | undefined;

packages/forms/signals/src/api/control_directive.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ElementRef,
1616
EventEmitter,
1717
inject,
18+
InjectionToken,
1819
Injector,
1920
Input,
2021
InputSignal,
@@ -41,6 +42,13 @@ import {FormCheckboxControl, FormUiControl, FormValueControl} from './control';
4142
import {AggregateProperty, MAX, MAX_LENGTH, MIN, MIN_LENGTH, PATTERN, REQUIRED} from './property';
4243
import type {Field} from './types';
4344

45+
/**
46+
* Lightweight DI token provided by the {@link Control} directive.
47+
*/
48+
export const CONTROL = new InjectionToken<Control<unknown>>(
49+
typeof ngDevMode !== undefined && ngDevMode ? 'CONTROL' : '',
50+
);
51+
4452
/**
4553
* Binds a form `Field` to a UI control that edits it. A UI control can be one of several things:
4654
* 1. A native HTML input or textarea
@@ -66,6 +74,10 @@ import type {Field} from './types';
6674
provide: NgControl,
6775
useFactory: () => inject(Control).ngControl,
6876
},
77+
{
78+
provide: CONTROL,
79+
useExisting: Control,
80+
},
6981
],
7082
})
7183
export class Control<T> {

0 commit comments

Comments
 (0)