1
我是新角度/角材料。角材料io MatDatePicker
我有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) 在MatDatepickerInput.registerDatepicker(datepicker.es5.js:1560)
感謝您的解決方案或反饋
Joost的
耶,我只是犯了這個錯誤。 'picker'在兩者之間必須是唯一的,只要它們是唯一的,請嘗試'[matDatepicker] =「picker1」'和'[matDatepicker] =「picker2」'或類似的。 '[for]'綁定也需要匹配新的id。 – stealththeninja