In Angular Material 22, how do you wire a mat-datepicker and its toggle to a text input, and what must the app provide first?
answer
- three pieces, one template reference
- the input carries the picker binding
- the toggle names its picker
- no date implementation, no datepicker
- a provide-adapter function in app config
basics
~10 sGive the mat-datepicker a template reference, bind the input with [matDatepicker]="picker" and the mat-datepicker-toggle with [for]="picker", and provide a DateAdapter, for example provideNativeDateAdapter(), in the app config; without one the datepicker throws.
solid answer
~40 sA Material datepicker is three cooperating pieces: the native `input` that holds the value, the `mat-datepicker` calendar pop-up, and an optional `mat-datepicker-toggle` button. You give the picker a template reference such as `#pickupPicker`, bind the input with `[matDatepicker]="pickupPicker"` and the toggle with `[for]="pickupPicker"`, and import `MatDatepickerModule` into the standalone component. The piece people forget is the date implementation: the datepicker reads, parses and formats every date through an injected `DateAdapter` and `MAT_DATE_FORMATS`, so the app config must call `provideNativeDateAdapter()` or the date-fns, Luxon or Moment provider function. In development mode, leaving it out throws `MatDatepicker: No provider found for DateAdapter`. Keep the toggle even though Alt + Down Arrow opens the calendar from the input, because some platforms swallow that shortcut.
code
ts · 9 linesimport {ApplicationConfig} from '@angular/core';
import {provideNativeDateAdapter} from '@angular/material/core';
export const appConfig: ApplicationConfig = {
providers: [
// Registers DateAdapter (NativeDateAdapter) and MAT_DATE_FORMATS
provideNativeDateAdapter(),
],
};go deeper
Recall the three pieces and the two bindings: [matDatepicker] on the input, [for] on the toggle, both pointing at the picker's template reference.
Explain why the datepicker needs an injected DateAdapter and MAT_DATE_FORMATS, what the provide-adapter functions register, and why they go in providers rather than imports.
Show you have debugged it: the missing-provider error, one picker per input, dateInput versus dateChange, and why the toggle stays for accessibility.
Frame the adapter as the seam that lets one calendar serve any date library, and decide where in the injector tree a product's date implementation should be provided.
## What a Material datepicker is made of Angular Material's datepicker is not a single element. It is a small set of pieces that find each other through a **template reference variable** and through **dependency injection**: - **The input**: a native `<input>` carrying the `matDatepicker` directive (selector `input[matDatepicker]`). It holds the value, parses what the user types, runs the date validators and plugs into `@angular/forms` through `formControl`, `formControlName` or `ngModel`. - **The pop-up**: `<mat-datepicker>`, the calendar panel. It renders nothing until it is opened. - **The toggle**: `<mat-datepicker-toggle>`, an optional icon button whose `for` input names the picker it opens. Its icon can be replaced by projecting an element marked `matDatepickerToggleIcon`. - **The date implementation**: a `DateAdapter` plus a `MAT_DATE_FORMATS` object, both provided by injection. Every date the other pieces read, parse, compare or display passes through them. ## Wiring it in a standalone component For a car-rental reservation page, the pick-up date field looks like this: ```ts import {Component} from '@angular/core'; import {MatDatepickerModule} from '@angular/material/datepicker'; import {MatFormFieldModule} from '@angular/material/form-field'; import {MatInputModule} from '@angular/material/input'; @Component({ selector: 'app-pickup-date', imports: [MatDatepickerModule, MatFormFieldModule, MatInputModule], template: ` <mat-form-field> <mat-label>Pick-up date</mat-label> <input matInput [matDatepicker]="pickupPicker"> <mat-datepicker-toggle matIconSuffix [for]="pickupPicker" /> <mat-datepicker #pickupPicker /> </mat-form-field> `, }) export class PickupDate {} ``` The reference `#pickupPicker` is the glue: the input and the toggle both receive the same `MatDatepicker` instance. The form field around the input is optional; the datepicker works on a bare input too. ## Providing the date implementation The component above compiles, but it cannot run until the application provides a date implementation: 1. Pick an adapter. `provideNativeDateAdapter()` from `@angular/material/core` works with JavaScript `Date` objects and adds no dependency. 2. For real parsing and locale support, use `provideDateFnsAdapter()`, `provideLuxonDateAdapter()` or `provideMomentDateAdapter()` from `@angular/material-date-fns-adapter`, `@angular/material-luxon-adapter` or `@angular/material-moment-adapter`. 3. Put the call in the `providers` array of `bootstrapApplication` or the app config. These functions return providers for `DateAdapter` and `MAT_DATE_FORMATS`; they are not components and do not belong in a component's `imports`. ## Errors you meet first | Message | Cause | Fix | |---|---|---| | `MatDatepicker: No provider found for DateAdapter` | no adapter provided anywhere in the injector tree | add a provide-adapter function | | `MatDatepicker: No provider found for MAT_DATE_FORMATS` | a custom `DateAdapter` provided without formats | provide `MAT_DATE_FORMATS` too | | `A MatDatepicker can only be associated with a single input.` | two inputs bind the same picker | one picker per input | | `Attempted to open an MatDatepicker with no associated input.` | the picker is opened but no input references it | bind an input with `[matDatepicker]` | The provider checks run in development mode; a production build without an adapter still breaks, only less legibly. ## Toggle and event details worth knowing - **Disabled state** flows from the input: the picker and toggle inherit it unless you set `disabled` on them directly, which lets you lock typing but allow calendar selection, or the reverse. - **Keyboard**: the input opens the calendar on Alt + Down Arrow, but the Material guide notes that ChromeOS intercepts that combination and that mobile screen-reader users have no way to open the dialog without the icon button. Keep the toggle. - **Events**: the input's native `(input)` and `(change)` events fire only for typing. Use `(dateInput)`, which fires whenever the value changes from typing or the calendar, and `(dateChange)`, which fires when typing ends on blur or when a calendar date is chosen. - **Value type**: what the input holds is decided by the adapter, a `Date` with the native and date-fns adapters, a Luxon `DateTime` or a `Moment` with the others.
- Why does the Material guide tell you to keep the datepicker toggle when Alt + Down Arrow already opens the calendar from the input?Because the shortcut is not reliable everywhere. ChromeOS intercepts Alt + Down Arrow at the operating-system level, so the browser only sees a Page Down key, and mobile screen-reader users have no way to trigger the calendar dialog without the icon button. The toggle is the one trigger that works for everyone.
- Why does a (change) handler on a datepicker input miss dates picked from the calendar, and what do you listen to instead?Native `input` and `change` events only fire from typing in the element. The datepicker input adds `(dateInput)`, which fires on every value change from typing or the calendar, and `(dateChange)`, which fires when typing is committed on blur or a calendar date is chosen. With a form control you can also watch the control's value.
- What happens if a return-date input and a pick-up input both bind [matDatepicker] to the same picker reference?The second registration throws `A MatDatepicker can only be associated with a single input.` Each input needs its own `mat-datepicker`. For a linked pick-up and return pair, use `mat-date-range-input` with one `mat-date-range-picker` instead.
- The datepicker works with no providers because dates are just JavaScript Date objects.
- The toggle finds its picker automatically because it sits inside the same form field.
- A native (change) listener on the input catches dates chosen from the calendar.
- Alt plus Down Arrow opens the calendar, so the toggle button is decoration.
- provideNativeDateAdapter() goes into a component's imports array next to MatDatepickerModule.