简体   繁体   中英

jQuery datepicker in Angular FormControl

I have a problem with binding value with DateTimePicker jQuery from xdsoft : https://xdsoft.net/jqplugins/datetimepicker/

I built FormControl using ReactiveFormsModule

I have <form [formGroup]="myForm">

And there are two inputs in it:

<input type="text" formControlName="name">
<input type="text" id="datetimepicker" formControlName="date" />

FormControl for "name" works fine.

However, FormControl for "date" doesn't show any values in:

{{ myform.value | json }}

StackBlitz:

https://stackblitz.com/edit/angular-mmsjj7?file=src/app/app.component.ts

GIF showing the problem: 在此处输入图片说明

Try this -

$('#datetimepicker').datetimepicker({
      timepicker: false,
      format: 'd.m.Y',
      onClose: (dateText, inst) => {
      this.myForm.controls['date'].setValue(dateText);
    }
    });

Update

Just remove form control from the date as we are assigning explicitly value to his control.

<input type="text" formControlName="name">
<input type="text" id="datetimepicker" />

Working example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM