67
67
As with other types of ` <input> ` , the datepicker works with ` @angular/forms ` directives such as
68
68
` formGroup ` , ` formControl ` , ` ngModel ` , etc.
69
69
70
- <!-- TODO - example -->
70
+ <!-- example(datepicker-value) -->
71
71
72
72
### Date validation
73
73
@@ -111,12 +111,7 @@ The `(dateInput)` event will fire whenever the value changes due to the user typ
111
111
date from the calendar. The ` (dateChange) ` event will fire whenever the user finishes typing input
112
112
(on ` <input> ` blur), or when the user chooses a date from the calendar.
113
113
114
- <!-- TODO - example -->
115
-
116
- ``` html
117
- <input [matDatepicker] =" d" (dateInput) =" onInput($event)" (dateChange) =" onChange($event)" >
118
- <mat-datepicker #d ></mat-datepicker >
119
- ```
114
+ <!-- example(datepicker-events) -->
120
115
121
116
### Disabling parts of the datepicker
122
117
@@ -126,7 +121,7 @@ their disabled state from the `<input>`, but this can be overridden by setting t
126
121
property on the datepicker or toggle elements. This can be useful if you want to disable text input
127
122
but allow selection via the calendar or vice-versa.
128
123
129
- <!-- TODO example -->
124
+ <!-- example(datepicker-disabled) -->
130
125
131
126
### Touch UI mode
132
127
@@ -169,21 +164,7 @@ export class MyApp {}
169
164
170
165
It's also possible to set the locale at runtime using the ` setLocale ` method of the ` DateAdapter ` .
171
166
172
- ``` ts
173
- import { DateAdapter , NativeDateAdapter } from ' @angular/material' ;
174
-
175
- @Component ({
176
- selector: ' foo' ,
177
- template: ' '
178
- })
179
- export class FooComponent {
180
- constructor (dateAdapter : DateAdapter <NativeDateAdapter >) {
181
- dateAdapter .setLocale (' de-DE' );
182
- }
183
- }
184
- ```
185
-
186
- <!-- TODO - example -->
167
+ <!-- example(datepicker-locale) -->
187
168
188
169
#### Choosing a date implementation and date format settings
189
170
@@ -224,10 +205,12 @@ the native `Date` object is the inability to set the parse format. We highly rec
224
205
` MomentDateAdapter ` or a custom ` DateAdapter ` that works with the formatting/parsing library of your
225
206
choice.*
226
207
227
- <!-- TODO - example -->
208
+ <!-- example(datepicker-moment) -->
228
209
229
210
#### Customizing the date implementation
230
211
212
+ <!-- TODO(mmalerba): Add a guide about this -->
213
+
231
214
The datepicker does all of its interaction with date objects via the ` DateAdapter ` . Making the
232
215
datepicker work with a different date implementation is as easy as extending ` DateAdapter ` , and
233
216
using your subclass as the provider. You will also want to make sure that the ` MAT_DATE_FORMATS `
@@ -244,27 +227,28 @@ provided in your app are formats that can be understood by your date implementat
244
227
export class MyApp {}
245
228
```
246
229
247
- <!-- TODO guide -->
248
-
249
230
#### Customizing the parse and display formats
250
231
251
232
The ` MAT_DATE_FORMATS ` object is just a collection of formats that the datepicker uses when parsing
252
233
and displaying dates. These formats are passed through to the ` DateAdapter ` so you will want to make
253
234
sure that the format objects you're using are compatible with the ` DateAdapter ` used in your app.
254
- This example shows how to use the ` NativeDateAdapter ` , but with custom formats.
235
+
236
+ If you want use one of the ` DateAdapters ` that ships with Angular Material, but use your own
237
+ ` MAT_DATE_FORMATS ` you can import the ` NativeDateModule ` or ` MomentDateModule ` . These modules are
238
+ identical to the "Mat"-prefixed versions (` MatNativeDateModule ` and ` MatMomentDateModule ` ) except
239
+ they do not include the default formats. For example:
255
240
256
241
``` ts
257
242
@NgModule ({
258
- imports: [MatDatepickerModule ],
243
+ imports: [MatDatepickerModule , NativeDateModule ],
259
244
providers: [
260
- {provide: DateAdapter , useClass: NativeDateAdapter },
261
245
{provide: MAT_DATE_FORMATS , useValue: MY_NATIVE_DATE_FORMATS },
262
246
],
263
247
})
264
248
export class MyApp {}
265
249
```
266
250
267
- <!-- TODO example -->
251
+ <!-- example(datepicker-formats) -->
268
252
269
253
#### Localizing labels and messages
270
254
@@ -282,8 +266,6 @@ application root module.
282
266
export class MyApp {}
283
267
```
284
268
285
- <!-- TODO example -->
286
-
287
269
### Accessibility
288
270
289
271
The ` MatDatepickerInput ` directive adds ` aria-haspopup ` attribute to the native input element, and it
@@ -349,4 +331,27 @@ will be added in future iterations, including:
349
331
350
332
### Troubleshooting
351
333
352
- <!-- TODO: fill in this section -->
334
+ #### Error: MatDatepicker: No provider found for DateAdapter/MAT_DATE_FORMATS
335
+
336
+ This error is thrown if you have not provided all of the injectables the datepicker needs to work.
337
+ The easiest way to resolve this is to import the ` MatNativeDateModule ` or ` MatMomentDateModule ` in
338
+ your application's root module. See the section on
339
+ [ choosing a date implementation] ( #choosing-a-date-implementation-and-date-format-settings ) ) for more
340
+ information.
341
+
342
+ #### Error: A MatDatepicker can only be associated with a single input
343
+
344
+ This error is thrown if more than one ` <input> ` tries to claim ownership over the same
345
+ ` <mat-datepicker> ` (via the ` matDatepicker ` attribute on the input). A datepicker can only be
346
+ associated with a single input.
347
+
348
+ #### Error: Attempted to open an MatDatepicker with no associated input.
349
+
350
+ This error occurs if your ` <mat-datepicker> ` is not associated with any ` <input> ` . To associate an
351
+ input with your datepicker, create a template reference for the datepicker and assign it to the
352
+ ` matDatepicker ` attribute on the input:
353
+
354
+ ``` html
355
+ <input [matDatepicker] =" picker" >
356
+ <mat-datepicker #picker ></mat-datepicker >
357
+ ```
0 commit comments