首页 > 解决方案 > Angular 4+ - 如何从输入日期选择器中获取日期

问题描述

我是角度的新手。

我有一个输入日期选择器。我想获取/获取在日期选择器中选择的日期。

代码:HTML文件:

 <input class="form-control m-input " id="m_datepicker_2_validate" placeholder="Select Date" readonly="" type="text">

我正在尝试 console.log 选定的日期。
在 component.ts 文件中,我添加了一个变量staff_date_booking : Date; 并创建了一个函数

bookStaffEmployeeDate(){
        console.log("This is the DATE:", this.staff_book_date);
    }

在我添加的 html 中

 <input [ngModel]="staff_book_date | date:'yyyy-MM-dd'" class="form-control m-input " id="m_datepicker_2_validate" placeholder="Select Date" readonly="" type="text">

选择日期后,我将在(单击)上调用此函数,但 this.staff_book_date 未定义。

如何将选定的日期存储在像 staff_book_date 这样的变量中

标签: angulardatepicker

解决方案


不要在里面使用管道ngModel。此外,使用中的双向绑定ngModel

<input [(ngModel)]="staff_book_date" 

推荐阅读