Description
I'm trying to wrap the MatDatePicker
in a custom component called MyDatePicker
so that I can use it in the html as the following.
Issue
The dateVariable
is always undefined and seems the two-way binding I implemented below is not working. Once the user selects a new date from the picker, the setter gets called. However, the new value is not binded to the dateVariable
.
Questions
- How can I implement a two-way binding between my custom date picker and a variable?
- How is it possible to implement a one-way binding (from date picker -> variable)?
Implementation
HTML file:
<my-datepicker [(selectedDate)]="dateVariable" placeholder="some text"></my-datepicker>
<button (click)="btnClick()">Show Selected Date</button>
The TS file:
//...
dateVariable: string;
btnClick() {
console.log('selected date:', this.dateVariable);
}
//...
MyDatepicker.component.ts
export class MyDatepickerComponent {
dateValue: string;
@Input() placeholder: string;
@Output() dateChange: EventEmitter<string> = new EventEmitter<string>();
@Input()
get selectedDate() {
console.log('getter');
return this.dateValue;
}
set selectedDate(val) {
console.log('setter');
this.dateValue = val;
this.dateChange.emit(this.dateValue);
}
pickerEvent(event: MatDatepickerInputEvent<Date>) {
this.selectedDate = event.value.toISOString();
}
}
MyDatepicker.component.html:
<mat-form-field>
<input matInput [matDatepicker]='pickerDate' placeholder='{{placeholder}}' (dateInput)="pickerEvent($event)">
<mat-datepicker-toggle matSuffix [for]='pickerDate'></mat-datepicker-toggle>
<mat-datepicker #pickerDate></mat-datepicker>
</mat-form-field>