Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facing issue 'A MatDatepicker can only be associated with a single input.' Angular 8 Material

<input matInput placeholder="DD/MM/YYYY" [matDatepicker]="picker" class="modifyDate" NoSpecialChar ngModel #dateCtrl="ngModel" name="datepicker" (click)="picker.open()" id="dtDeparture" required>
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
<mat-error *ngIf="dateCtrl.errors?.required && dateCtrl.touched">Choose a Date       
</mat-error>

<input matInput placeholder="DD/MM/YYYY" [matDatepicker]="picker1" NoSpecialChar class="modifyDate" [(ngModel)]="inputEndDate" name="dtArrival" (click)="picker1.open()" id="dtArrival" required>
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker #picker1></mat-datepicker>

As you can see above we have unique ids for both the date inputs viz picker1 and picker. Still the issue comes 'A MatDatepicker can only be associated with a single input.' from nowhere. I need help. I searched on google and also on stackover but no help

like image 287
Ankita Avatar asked Dec 20 '19 09:12

Ankita


People also ask

How do I disable matDatepicker?

To disable only datepicker toggle we can add dispabled property to the mat-datepicker-toggle element. In this case user can only enter date by manually typing.

How to disable datepicker textbox in angular?

By default, the DatePicker is enabled. To disable the component, set its disabled property to true .


1 Answers

Try this replace your code with this and try

<mat-form-field>
  <input matInput [matDatepicker]="picker">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

<mat-form-field>
  <input matInput [matDatepicker]="picker1">
  <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
  <mat-datepicker #picker1></mat-datepicker>
</mat-form-field>

refers to the component that the is wrapping (e.g. the input, textarea, select, etc.)

like image 110
mayur Avatar answered Oct 04 '22 12:10

mayur