1
votes

I am using Angular and material datepicker with Reactive Forms and moment MomentDateModule. How can I get the value from the form that is typed by the keyboard?

Here is a small example. If I am typing the correct date, then valueChanges is emitted with correct moment object and everything is ok. With invalid date there is null in this.date.value control

Is there a way to store current input value in Form Control with Reactive Forms? I also tried to add (input)="onInput($event)" to datepicker <input />

onInput(event) { this.date.setValue(moment(event.target.value)) }

But it didn't work. Because datepicker converts invalid moment objects to empty strings for the input value

1

1 Answers

0
votes

If I got you right, you can do it using inputs' value like this.

Your declaration: @ViewChild('input', { static: false }) input: ElementRef

Usage: this.input.nativeElement.value