2

I'am coding with angular 7 and bootstrap 4. I used bootstrap datepicker to select date and trying to get data from it .

My html code is like that :

<div class="col-lg-8 form-group">
      <input id="startDate" type="text" placeholder="From" 
      class="form-control" bsDatepicker
      [bsConfig]="{ adaptivePosition: true, 
      dateInputFormat:'YYYY-MM-DD'}" (change)="onDateSelect($event)" >
</div>

and the JavaScript function is:

 selectedStartDate: string;
 onDateSelect(event) {
     this.selectedStartDate = event.target.value;
     console.log(this.selectedStartDate)
   }

I'm getting "undefined" as a result.

selectedStartDate = undefined

can anyone help me to get the value of the selected date from the bootstrap datepicker ?

4
  • <div class="col-lg-8 form-group"> <input id="startDate" type="text" placeholder="From" class="form-control" bsDatepicker [bsConfig]="{ adaptivePosition: true, dateInputFormat:'YYYY-MM-DD'}" #value (change)="onDateSelect(value.value)" > </div> //Now this function will have value selectedStartDate: string; onDateSelect(event) { this.selectedStartDate = event.target.value; console.log(this.selectedStartDate) } Commented Jul 24, 2019 at 13:38
  • It didn't work ! Commented Jul 24, 2019 at 13:46
  • Did you tried console the parameter itself ,(event.target.value) this will not work Commented Jul 24, 2019 at 13:58
  • Yes and it was "undefined" too. the solution proposed by ahmeticat below worked. Commented Jul 24, 2019 at 14:08

2 Answers 2

1

You can use with ngModel property and ngModelChange function.

For example change your html

<input id="startDate" type="text" placeholder="From" 
      class="form-control" bsDatepicker [(ngModel)]="selectedStartDate"
      [bsConfig]="{ adaptivePosition: true, 
      dateInputFormat:'YYYY-MM-DD'}" (ngModelChange)="updateMyDate($event)" >

and ts file

updateMyDate(newDate) {
    console.log(newDate);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it worked ! But can you please explain the need of "selectedStartDate" in HTML while I'm not really using it in the TS file following your solution?
I set selectedStartDate is a ngModel and we can use ngModelChange.So angular trigger to our ngModelChange when model changed
0
<div class="col-lg-8 form-group">
      <input id="startDate" type="text" placeholder="From" 
      class="form-control" bsDatepicker [(ngModel)]="selectedStartDate"
      [bsConfig]="{ adaptivePosition: true, 
      dateInputFormat:'YYYY-MM-DD'}" >
</div>

you will have date in "selectedStartDate"

1 Comment

I tried that and didn't work. Also I don't really need a two way binding as all what I need is to get the selected date from the template.

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.