Wire a ControlValueAccessor like Angular forms: setUpControl, updateOn blur and submit, and IME input
Problem
Every custom form control in Angular (a date picker, a star rating, a phone input) implements ControlValueAccessor: writeValue, registerOnChange, registerOnTouched and optionally setDisabledState. Senior interviews ask what Angular does with those four methods: when each is called, why typing does not echo back into the input, how updateOn: 'blur' works, and why setDisabledState is suddenly called on enabled controls after an upgrade. Build the plumbing between a control and an accessor, following setUpControl in @angular/forms.
const name = new FormControl("", { updateOn: "blur" });
setUpControl(name, new DefaultValueAccessor(input));
// typing "Ravi": name.value is still "", nothing emitted, the input shows "Ravi"
// blur: name.value === "Ravi", dirty, touched, valueChanges emitted onceRef. UR-1457 · © UIRound · uiround.com/questions/wire-a-controlvalueaccessor-like-angular-forms-setupcontrol-updateon-blur-and-submit-and-ime-input
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
- 3
- code listings
- 8
- edge cases
- 5
- follow-up questions
What's inside
- The full problem, with examples
- Tests that check your solution
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Card worldwide
- UPI and net banking in India
- One-time payment, nothing renews
Stuck? Take a hint
- 1There are two directions. View to model: the accessor calls the function you gave to
registerOnChange. Model to view: the control callswriteValue. A value that came from the view must not be written back, so passemitModelToViewChange: falsewhen the view updates the model.
Try it in the editor
Run your code as often as you like. Submitting it to the full test suite is part of Pro.
Private, and saved in this browser as you type. All your notes are on My lists.