Question presented to candidate: "An edit-profile page loads a user from the API and fills a FormGroup. Sometimes the API leaves out a field and the page crashes, sometimes it does not. Explain the difference between setValue() and patchValue(), and which one you would use here."
What a strong answer should cover:
setValue()requires the exact shape: every control key must be present, or it throwsNG01002: Must supply a value for form control with name: '...'; an unknown key throwsNG01001.patchValue()accepts a partial object, updates only the keys it recognises and silently ignores the rest.- With typed forms (v14+), TypeScript already rejects a partial object passed to
setValue(); the runtime check is the safety net for untyped data such as API responses. - Disabled controls still count:
setValueneeds their key too, andform.valueomits them whilegetRawValue()includes them. - Both work recursively on nested groups and arrays, both fire
valueChangesonce for the group, and both accept{ emitEvent: false }. reset()is different again: it restores initial values fornonNullablecontrols and sets the rest tonull.
Clarifying questions expected:
- "Is the API response guaranteed to have every field, or should missing fields keep their current values?"
- "Do we want the page to fail loudly if the response shape changes?"
Code / implementation expected: Yes β call both on the same typed FormGroup and show the error text and the resulting values.