Reputation: 117
I want add a date picker to select a date range.
MatFormFieldModule,
MatDatepickerModule,
MatInputModule,
<mat-form-field appearance="fill">
<mat-label>Enter a date range</mat-label>
<mat-date-range-input [rangePicker]="picker">
<input matStartDate placeholder="Start date" #arrival>
<input matEndDate placeholder="End date" #departure>
</mat-date-range-input>
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-date-range-picker #picker></mat-date-range-picker>
</mat-form-field>
But I receive the following error:
ERROR Error: mat-form-field must contain a MatFormFieldControl.
Upvotes: 2
Views: 10038
Reputation: 24114
The Angular Material docs provide the following error description:
Error: mat-form-field must contain a MatFormFieldControl
This error occurs when you have not added a form field control to your form field. If your form field contains a native
<input>
or<textarea>
element, make sure you've added thematInput
directive to it and have importedMatInputModule
. Other components that can act as a form field control include<mat-select>
,<mat-chip-list>
, and any custom form field controls you've created.
The matInput
directive needs to be added to the <input>
elements.
To select a date range using two date-pickers, place each date-picker within its own mat-form-field
. See Angular example Datepicker palette colors
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>Enter a date range</mat-label>
<input matInput [matDatepicker]="picker">
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
Upvotes: 2