Sub topic 9
Working with select, radio, and checkbox inputs
This chapter assumes you can bind a text input with [(ngModel)] inside a standalone component that imports FormsModule. It also assumes you know what a name attribute does on a control inside a form. Here you extend that pattern to select, radio, and checkbox controls.
How ngModel picks a value accessor
ngModel does not read the DOM directly. Each control type has a value accessor that translates between the DOM and the model. Angular picks the accessor from the element type, so the same [(ngModel)] syntax works everywhere. What differs is the model value you get back.
| Control | Model value | Typical use |
|---|---|---|
| `<select>` | The selected option's value | Choose one item from a list |
| Radio group | The value of the checked radio | Choose one item from a few visible options |
| Single checkbox | `true` or `false` | Accept terms, toggle a flag |
| Several checkboxes | You assemble an array yourself | Pick many items |
Select
Bind [(ngModel)] on the <select>. Use [ngValue] on each <option> when the value is not a plain string, such as a number or an object. [value] always produces a string.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
interface Country {
code: string;
name: string;
}
@Component({
selector: 'app-profile',
imports: [FormsModule],
template: `
<form #f="ngForm" (ngSubmit)="onSubmit()">
<label for="country">Country</label>
<select id="country" name="country" [(ngModel)]="model.country" required>
<option [ngValue]="null" disabled>Choose a country</option>
@for (c of countries; track c.code) {
<option [ngValue]="c">{{ c.name }}</option>
}
</select>
<fieldset>
<legend>Plan</legend>
@for (p of plans; track p) {
<label>
<input type="radio" name="plan" [(ngModel)]="model.plan" [value]="p" />
{{ p }}
</label>
}
</fieldset>
<label>
<input type="checkbox" name="terms" [(ngModel)]="model.terms" required />
I accept the terms
</label>
<button type="submit" [disabled]="f.invalid">Save</button>
</form>
<pre>{{ model | json }}</pre>
`,
})
export class ProfileComponent {
countries: Country[] = [
{ code: 'NO', name: 'Norway' },
{ code: 'KE', name: 'Kenya' },
];
plans = ['Free', 'Pro'];
model: { country: Country | null; plan: string; terms: boolean } = {
country: null,
plan: 'Free',
terms: false,
};
onSubmit() {
console.log('Submitted', this.model);
}
}The json pipe needs JsonPipe in the component's imports. Add JsonPipe to the array and import it from @angular/common, or remove the <pre> line.
With [ngValue], model.country holds the whole Country object, not just its code. Angular matches the selected option by object identity. If you reload data and the new objects are different instances, the select shows no selection. Fix this with [compareWith] on the select, passing a function that compares by code.
Radio buttons
Radios in one group share the same name and the same [(ngModel)] expression. Angular uses name to tie them together, so a missing or mismatched name breaks the group. Each radio sets its own [value]. When the user checks one, the model receives that radio's value.
Checkboxes
A single checkbox binds to a boolean. Adding required to a checkbox does not mean "must have a value". Angular's required validator on a checkbox only passes when the box is checked. For a checkbox that must be ticked, such as accepting terms, use required as shown above.
Several checkboxes for one list have no built-in array binding. Give each its own boolean in the model, or manage an array in the component with (change) and [checked].
<label>
<input type="checkbox" name="news" [(ngModel)]="model.news" /> Newsletter
</label>
<label>
<input type="checkbox" name="tips" [(ngModel)]="model.tips" /> Tips
</label>Common errors
- A control without a
nameinside a form throws an error at runtime, becauseNgFormregisters each control under its name. - Using
[value]with objects gives you[object Object]strings. Use[ngValue]. - Starting a select with
undefinedshows a blank option. Start withnulland add a disabled placeholder option, as above.
Exercise
A select lists objects with [value]="c", and the model ends up holding the text "[object Object]". What do you change, and why?
Show answer
Replace [value]="c" with [ngValue]="c". [value] sets a DOM string attribute, so objects are converted to strings. [ngValue] lets the select's value accessor keep the original object.
Exercise
Two radio buttons bind to model.plan but one has name="plan" and the other has name="plans". What goes wrong?
Show answer
Angular treats them as separate controls, and the browser no longer groups them as one set. Give every radio in the group the same name.