Sub topic 17
Disabled Controls and getRawValue
This chapter assumes you can already create typed FormControl and FormGroup instances, bind them with formControlName, and update values with setValue and patchValue.
How Disabled Controls Behave
A disabled control stays in the form model but drops out of two things:
- Value: the parent's
valueleaves it out. - Validity: its validators do not run, and the parent's status ignores it.
You can start a control disabled with a boxed value, or toggle it later with disable() and enable().
import { FormControl, FormGroup } from '@angular/forms';
const account = new FormGroup({
id: new FormControl({ value: 'u-42', disabled: true }, { nonNullable: true }),
email: new FormControl('[email protected]', { nonNullable: true }),
});
console.log(account.value); // { email: '[email protected]' }
console.log(account.getRawValue()); // { id: 'u-42', email: '[email protected]' }
account.controls.email.disable();
console.log(account.status); // 'DISABLED' (every child is disabled)
account.controls.email.enable();
console.log(account.status); // 'VALID'Because any control might be disabled, typed forms declare value as Partial<...>. The getRawValue() method returns the full, non-partial type. Use it when you submit a payload that must include read-only fields.
| Property or method | Includes disabled controls | Type |
|---|---|---|
| `form.value` | No | `Partial<T>` |
| `form.getRawValue()` | Yes | `T` |
The disable() and enable() methods emit valueChanges and statusChanges by default. Pass { emitEvent: false } to toggle a control from inside a subscription without setting off a loop.
Building Dynamic Forms at Runtime
Sometimes the shape of a form is only known at runtime, for example notification toggles loaded from an API. Use FormRecord for this. It is a group whose keys are any strings, and all of its controls share one type.
import { FormControl, FormRecord } from '@angular/forms';
const prefs = new FormRecord<FormControl<boolean>>({});
for (const topic of ['news', 'offers', 'security']) {
prefs.addControl(topic, new FormControl(false, { nonNullable: true }));
}
prefs.removeControl('offers');
console.log(prefs.contains('offers')); // false
console.log(prefs.value); // { news: false, security: false }When most of the keys are fixed and only a few are optional, keep a typed FormGroup and mark those keys optional. TypeScript then allows addControl and removeControl only for the optional keys.
import { FormControl, FormGroup } from '@angular/forms';
interface ContactForm {
email: FormControl<string>;
phone?: FormControl<string>;
}
const contact = new FormGroup<ContactForm>({
email: new FormControl('', { nonNullable: true }),
});
contact.addControl('phone', new FormControl('', { nonNullable: true }));
contact.removeControl('phone');
// contact.removeControl('email'); // compile error: 'email' is requiredTo render a FormRecord, loop over Object.keys(prefs.controls) inside the [formGroup] element and bind each input with [formControlName]="key".
| Need | Use |
|---|---|
| Ordered list of similar items | `FormArray` |
| Unknown string keys, same control type | `FormRecord` |
| Fixed shape with a few optional fields | `FormGroup` with optional keys |
Exercise
A form has name (enabled, value 'Ana') and createdAt (disabled, value '2026-01-01'). What do form.value and form.getRawValue() return, and which one should you send to an API that requires both fields?
Show answer
form.value returns { name: 'Ana' } and form.getRawValue() returns { name: 'Ana', createdAt: '2026-01-01' }. Send getRawValue(), because value leaves out disabled controls.
Exercise
You load a list of feature flag names from a server and need one boolean checkbox per flag. Which container fits, and how do you add each control?
Show answer
Use new FormRecord<FormControl<boolean>>({}) and call addControl(name, new FormControl(false, { nonNullable: true })) for each flag name.