Wire a ControlValueAccessor like Angular forms: setUpControl, updateOn blur and submit, and IME input

hardAbout 50 minutesUpdated CodingProangularformscontrol-value-accessorreactive-formsdom-eventsi18n
Not solved

Share this question

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 once

Ref. UR-1457 · © UIRound · uiround.com/questions/wire-a-controlvalueaccessor-like-angular-forms-setupcontrol-updateon-blur-and-submit-and-ime-input

Stuck? Take a hint

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.

Every question and answer7 days, one payment