0

I am trying to get the value from a date picker (ng2-date-picker) input field, but might be missing something. I have tried all sorts of different ways to get the value, but with little success.

This is the link to the npm package I am using.

This is my date picker setup in my component:

import { DatePickerComponent } from 'ng2-date-picker';

  @ViewChild('dayPicker', {static: false}) datePicker: DatePickerComponent;
  open() { this.datePicker.api.open(); }
  close() { this.datePicker.api.close(); }

This is the date picker setup in my view:

    <dp-date-picker
        #dayPicker
        name="dob"
        [(ngModel)]="dateOfBirth"
        [config]='dateConfig'>
    </dp-date-picker>

I have tried accessing the value using the reference - #dayPicker and ngModel using dateOfBirth, but both values are undefined even after a date has been selected.

console.log('DOB: ' + this.dateOfBirth); (undefined)
console.log('this.datePicker: ' + this.datePicker); (undefined)

The only way I have been able to access the value, was through the following (but this only updates on the blur event, so I only get a value after the 2nd time the date is changed).

this.datepickerInputValue = (<HTMLInputElement><any>document.getElementsByClassName('dp-picker-input'))[0].value;
console.log('input value: ' + this.datepickerInputValue);

Thank you in advance!

2
  • Any errors in he console when using ngModel? Commented Jun 24, 2020 at 13:35
  • Nope, I don't get any errors. Tried all variations of ngModel. But still get an undefined value Commented Jun 24, 2020 at 13:43

1 Answer 1

1

Have you tried (onChange) or (onSelect) on your datepicker tag? If none of those 2 works try this (ngModelChange). They should work btw.

Sign up to request clarification or add additional context in comments.

7 Comments

How, and where would the OP make these changes?
what do you mean? OP?
I was able to get the required value using (ngModelChange)="dateOfBirthChange($event)". However, I'm still perplexed why ngModel and the reference does not work....
Yeah it happened with me as well quite a while back. They have mentioned in the documentation as well " if you want to receive every value (valid and invalid) please use the native ngModelChange output. "
But I am glad it helped you solve the problem :). If you think it did mark it as an answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.