Question presented to candidate: "Design wants a five-star rating widget that behaves like a normal input inside our reactive forms: it should take a value from the FormControl, report changes, mark the control touched, and respect disable(). How do you build it with ControlValueAccessor?"
What a strong answer should cover:
ControlValueAccessor(CVA) is the adapter interface between a forms directive (formControl,formControlName,ngModel) and any UI element.- The four methods and their direction:
writeValue(model to view),registerOnChangeandregisterOnTouched(store callbacks for view to model), optionalsetDisabledState. - Register it with
{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => StarRating), multi: true }. - Call the stored
onChangeonly for user changes, never insidewriteValue; callonTouchedon blur. - Know the call order: reactive directives call
writeValuefirst, thensetDisabledState(always, since v15's default), then the register methods;ngModelfirst writesnull, then the real value a tick later. - Handle
null(fromreset()), and keep internal state in signals so the OnPush view updates. Bonus: Angular 22 can also drive a CVA-freemodel()-based control.
Clarifying questions expected:
- "Should the widget support keyboard input and be focusable for accessibility?"
- "What should the value be when the form is reset: 0 or null?"
Code / implementation expected: Yes β a working CVA component used with a FormControl, ideally logging when each method is called.