Full answer
Angular forms talk to native inputs through built-in value accessors. A custom control, such as a star rating or a date picker, needs to speak the same protocol. ControlValueAccessor (CVA) is that bridge, with four methods:
writeValue(value): the forms API pushes a value into the component, for example fromsetValueor the initial value.registerOnChange(fn): storefnand call it when the user changes the value inside the component.registerOnTouched(fn): storefnand call it when the user leaves the control, sotouchedbecomes true.setDisabledState(isDisabled): optional. Called when the control is disabled or enabled.
Then register the component as an accessor: providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => RatingInputComponent), multi: true }]. multi is needed because there are many accessors on the page. Use useExisting with forwardRef, since the class is referenced inside its own decorator before it is defined.
After that, <app-rating-input formControlName="rating" /> or [(ngModel)] works. Validators such as Validators.min(3) on the control apply to the value your component writes.
Signals make the internal state tidy: a signal for the value and one for the disabled state, and the template reads both.
Important: do not call onChange from writeValue. That is the form pushing a value in, and calling back creates a loop and marks the control dirty.
Signal-based forms are being developed in newer Angular versions, but CVA remains the stable, supported way in Angular 19 and 20.
import { Component, forwardRef, signal } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
@Component({
selector: 'app-rating-input',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => RatingInputComponent),
multi: true,
},
],
template: `
@for (n of [1, 2, 3, 4, 5]; track n) {
<button
type="button"
[disabled]="disabled()"
[attr.aria-label]="n + ' stars'"
[attr.aria-pressed]="n === value()"
(click)="select(n)"
(blur)="onTouched()"
>
{{ n <= value() ? '*' : '-' }}
</button>
}
`,
})
export class RatingInputComponent implements ControlValueAccessor {
readonly value = signal(0);
readonly disabled = signal(false);
private onChange: (value: number) => void = () => {};
onTouched: () => void = () => {};
writeValue(value: number | null): void {
this.value.set(value ?? 0); // Do not call onChange here.
}
registerOnChange(fn: (value: number) => void): void {
this.onChange = fn;
}
registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this.disabled.set(isDisabled);
}
select(n: number): void {
this.value.set(n);
this.onChange(n);
}
}Angular 20.3, strict template check passes. Use `<app-rating-input formControlName="rating" />` inside a reactive form.
Key points
- Implement `writeValue`, `registerOnChange`, `registerOnTouched`, and `setDisabledState`.
- Provide `NG_VALUE_ACCESSOR` with `useExisting: forwardRef(...)` and `multi: true`.
- Call the stored `onChange` when the user changes the value, and `onTouched` on blur.
- Do not call `onChange` inside `writeValue`.
Common mistakes
- Forgetting `multi: true`, which replaces the built-in accessors.
- Calling `onChange` from `writeValue`, creating loops and wrong dirty state.
- Not handling `setDisabledState`, so a disabled form still lets users click.
Follow-ups
- Why `useExisting` and `forwardRef` instead of `useClass`?
- How would you add validation inside the custom control?
- How do you make the control work with `ngModel` as well?
Tips
- Walk through the data direction of each method: in, out, touched, disabled.
- Explain the `forwardRef` reason briefly. It is a common follow-up.
- Mention accessibility: buttons need labels and keyboard support.
Evaluation rubric
- Implements all four methods correctly.
- Registers the provider properly.
- Avoids the writeValue loop.