Components: can i use two MdDatepicker in one component?

Created on 1 Nov 2017  路  8Comments  路  Source: angular/components

This is my code :

<md-input-container class="example-full-width">
      <input class="full-width" mdInput formControlName="JoinDateUtc" [mdDatepicker]="picker" placeholder="Join Date">
      <md-datepicker-toggle mdSuffix [for]="picker"></md-datepicker-toggle>
      <md-datepicker #picker></md-datepicker>
</md-input-container>

<md-input-container class="example-full-width">
      <input class="full-width" mdInput formControlName="LeftDateUtc" [mdDatepicker]="picker" placeholder="Left Date">
      <md-datepicker-toggle mdSuffix [for]="picker"></md-datepicker-toggle>
      <md-datepicker #picker></md-datepicker>
</md-input-container>

and i get an error :
Error: An MdDatepicker can only be associated with a single input.

Most helpful comment

@willshowell just use different ID for each
Example:

<md-input-container class="example-full-width">
      <input class="full-width" mdInput formControlName="JoinDateUtc" [mdDatepicker]="picker" placeholder="Join Date">
      <md-datepicker-toggle mdSuffix [for]="picker"></md-datepicker-toggle>
      <md-datepicker #picker></md-datepicker>
</md-input-container>

<md-input-container class="example-full-width">
      <input class="full-width" mdInput formControlName="LeftDateUtc" [mdDatepicker]="pickerTo" placeholder="Left Date">
      <md-datepicker-toggle mdSuffix [for]="pickerTo"></md-datepicker-toggle>
      <md-datepicker #pickerTo></md-datepicker>
</md-input-container>

I changed the ID to pickerTo at three places

[mdDatepicker]="pickerTo"
mdSuffix [for]="pickerTo"
<md-datepicker #pickerTo>

All 8 comments

solved. thanks

@arisbro8 How did you solve it?

@Werner-Dohse probably by creating two separate mat-datepickers and assigning them different template references.

@willshowell just use different ID for each
Example:

<md-input-container class="example-full-width">
      <input class="full-width" mdInput formControlName="JoinDateUtc" [mdDatepicker]="picker" placeholder="Join Date">
      <md-datepicker-toggle mdSuffix [for]="picker"></md-datepicker-toggle>
      <md-datepicker #picker></md-datepicker>
</md-input-container>

<md-input-container class="example-full-width">
      <input class="full-width" mdInput formControlName="LeftDateUtc" [mdDatepicker]="pickerTo" placeholder="Left Date">
      <md-datepicker-toggle mdSuffix [for]="pickerTo"></md-datepicker-toggle>
      <md-datepicker #pickerTo></md-datepicker>
</md-input-container>

I changed the ID to pickerTo at three places

[mdDatepicker]="pickerTo"
mdSuffix [for]="pickerTo"
<md-datepicker #pickerTo>

Use like this :
<mat-form-field> <input matInput [matDatepicker]="picker1" placeholder="Choose a start date" [(ngModel)]="inputStartDate"> <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle> <mat-datepicker #picker1 [startAt]="startDate"></mat-datepicker> </mat-form-field>

<mat-form-field> <input matInput [matDatepicker]="picker2" placeholder="Choose an end date" [(ngModel)]="inputEndDate"> <mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-toggle> <mat-datepicker #picker2 [startAt]="endDate"></mat-datepicker> </mat-form-field>

The problem is solved after using the code like this:

(i.e.) Changing picker to picker2 when used for the second time.

<md-input-container class="example-full-width">
      <input class="full-width" mdInput formControlName="JoinDateUtc" [mdDatepicker]="picker" placeholder="Join Date">
      <md-datepicker-toggle mdSuffix [for]="picker"></md-datepicker-toggle>
      <md-datepicker #picker></md-datepicker>
</md-input-container>

<md-input-container class="example-full-width">
      <input class="full-width" mdInput formControlName="LeftDateUtc" [mdDatepicker]="picker2" placeholder="Left Date">
      <md-datepicker-toggle mdSuffix [for]="picker2"></md-datepicker-toggle>
      <md-datepicker #picker2></md-datepicker>
</md-input-container>

Thank you so much. It helped me :)

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

michaelb-01 picture michaelb-01  路  3Comments

kara picture kara  路  3Comments

alanpurple picture alanpurple  路  3Comments

shlomiassaf picture shlomiassaf  路  3Comments

3mp3ri0r picture 3mp3ri0r  路  3Comments