mezoistvan / ng2-flatpickr

Angular 2+ wrapper for flatpickr (https://github.com/chmln/flatpickr)
100 stars 53 forks source link

Setting an initial value with a formgroup yields no result #48

Open enko opened 5 years ago

enko commented 5 years ago

I'm creating a FormGroup with a field birthday and want to hook it up with a ng2-flatpickr component. If I select a date the formgroup now has the correct value. If I try to set an initial value with this.form.patchValue({ birthday: new Date() }); the flatpickr is empty. I would expect that the flatpickr shows the current date.

Here is my repro-repo: https://github.com/enko/ng2-flatpickr-problem

Relevant files are https://github.com/enko/ng2-flatpickr-problem/blob/master/src/app/app.component.ts and https://github.com/enko/ng2-flatpickr-problem/blob/master/src/app/app.component.html

Picktarget commented 5 years ago

I just meet the same problem!!!

AbuzerAsif commented 5 years ago

@mezoistvan If you need help with this issue, then you can assign it to me. I am here to help...

dockleryxk commented 5 years ago

I had this issue, here is what I did to resolve it:

I attach it to an input like this:

<input formControlName="datePickerDate"
       [flatpickr]="datePickerOptions"
       name="datePickerDate">

Then in the component ngOnInit:

this.datePickerOptions = nothingSpecial;
// this is the important part
// initialize the date here
this.datePickerOptions.defaultDate = new Date();

For some reason adding the default date after creating the object made it work for me. ¯\(ツ)/¯ I don't know if it helps anyone else but I hope it does!

rmckeel commented 4 years ago

@AbuzerAsif If you can fix it, would love to see this issue fixed! Ran into this issue today and couldn't generate a proper fix to it (making reactive form patchValue work as intended) in the time budgeted for our project task. So this is still a current/relevant bug.

naimmalek commented 4 years ago

@dockleryxk

Thank you. its working.