I have problem because I do not know how to fire a change event after someone picks a date.
My current code:
<div class="d-flex align-items-center">
<mat-form-field appearance="fill">
<mat-label>Wybierz przedział czasowy
</mat-label>
<mat-date-range-input [formGroup]="range" [rangePicker]="picker">
<input matStartDate formControlName="start" placeholder="Data początkowa">
<input matEndDate formControlName="end" placeholder="Data końcowa">
</mat-date-range-input>
<mat-datepicker-toggle matSuffix [for]=" picker"></mat-datepicker-toggle>
<mat-date-range-picker #picker></mat-date-range-picker>
<mat-error *ngIf="range.controls.start.hasError('matStartDateInvalid')">Zła data początkowa</mat-error>
<mat-error *ngIf="range.controls.end.hasError('matEndDateInvalid')">Zła data końcowa</mat-error>
</mat-form-field>
</div>
For input event, Angular Material provides (dateInput) event for Datepicker. The dateInput triggers when user enters date manually in input box as well as selecting date from calendar.
First, open the app.component.ts file; at the top, import the FormControl and FormGroup module from ‘@angular/forms’. Also, define a dateRange form with start and end value in addition to FormControl API.
Declare the mat-form-field directive to invoke the form, and the mat-date-range-input directive brings the date range picker in the view, not just that mention start and end date input along with the mat-datepicker-toggle directive. Ultimately, every thing has been done which is required to implement date range picker in angular.
How to detect when an @Input () value changes in Angular? 1 First, we will look at Two-way binding.#N#Two-way binding combines input and output in a single notation using ngModel... 2 Implementation of One-way binding with ngOnChange () and @Input ():#N#Here is how we will use ngOnChange () to bind the... More ...
you can know when change in differents ways
using (dataChange) or (dateInput) in each input:
<mat-date-range-input [formGroup]="range" [rangePicker]="picker">
<input matStartDate formControlName="start" placeholder="Start date" (dateChange)="startChange($event)">
<input matEndDate formControlName="end" placeholder="End date" (dateChange)="endChange($event)">
</mat-date-range-input>
startChange(event:MatDatepickerInputEvent)
{
console.log("startChange",event.value)
}
endChange(event:MatDatepickerInputEvent)
{
console.log("endChange",event.value)
}
Of subscribing to valueChanges of the FormGroup/FormControl. e.g. in ngOnInit
ngOnInit()
{
this.range.get('start').valueChanges.subscribe(res=>{
console.log("valueChange start",res)
})
this.range.get('end').valueChanges.subscribe(res=>{
console.log("valueChange end",res)
})
this.range.valueChanges.subscribe(res=>{
console.log("range Change",res)
})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With