我是Angular / Angular Material的新手。
我有一个带有2个MatDatePicker输入字段的div:
<div class="docs-example-viewer-body">
<mat-form-field>
<mat-select placeholder="Employee" [(ngModel)]="selectedEmployeeId" name="employee_name" (ngModelChange)="setSelectedEmployee($event)">
<mat-option *ngFor="let employee of employees" [value]="employee.id">
{{employee.name}}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<input matInput [matDatepicker]="picker" placeholder="Choose a start date" [(ngModel)]="inputStartDate">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker [startAt]="startDate"></mat-datepicker>
</mat-form-field>
<mat-form-field>
<input matInput [matDatepicker]="picker" placeholder="Choose an end date" [(ngModel)]="inputEndDate">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker [startAt]="endDate"></mat-datepicker>
</mat-form-field>
<p *ngIf="selectedEmployeeId"> Selected value: {{selectedEmployeeId}} from {{inputStartDate}} until {{inputEndDate}}</p>
</div>
但是我在浏览器中收到以下错误:
MatDatepicker只能与单个输入相关联。在 MatDatepicker._registerInput(datepicker.es5.js:1281) at MatDatepickerInput.registerDatepicker(datepicker.es5.js:1560)
感谢您的解决方案或反馈
Joost的
答案 0 :(得分:14)
实例变量名称必须对每个实例都是唯一的。在示例中,我使用了picker1
和picker2
,但可以随意使用更好的名称。 Here's a sample from the Material plunker.
<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>