angular2 和 bootstrap-datepicker -- 如何获取选定的日期?

angular2 and bootstrap-datepicker -- how to get the selected date?

我正在尝试在 angular2 中使用 bootstrap-datepicker。

                <div class="input-append date" id="datepicker">
                    <input class="span2" size="16" type="text" />
                    <span class="add-on"><i class="icon-calendar"></i></span>
                </div>





    $('#datepicker').datepicker({
        format: 'yyyy-mm-dd',
        startDate: '2001-01-01',
        autoclose: true
    }).on('changeDate', function(ev){
        this.stupid_date = ev.date;
        console.log('dudeme',ev.date.valueOf(),ev.date )
    });

如何获取选定的日期? console.log 打印但 this.stupid_date 未更新。

您的回调函数中的 this 似乎没有引用您的组件实例。

尝试使用箭头函数如下:

}).on('changeDate', (ev) => {
  this.stupid_date = ev.date;
  console.log('dudeme',ev.date.valueOf(),ev.date )
});

Plunker Example

另见