简体   繁体   English

bootstrap-datepicker在选择日期时不会更新AngularJS模型

[英]bootstrap-datepicker does not update AngularJS model when picking date

I am using AngularJS with this bootstrap-datepicker plugin : 我在这个bootstrap-datepicker插件中使用AngularJS:

Datepicker for Bootstrap v1.6.4 ( https://github.com/eternicode/bootstrap-datepicker ) Bootstrap v1.6.4的Datepicker( https://github.com/eternicode/bootstrap-datepicker

Copyright 2012 Stefan Petre 版权所有2012 Stefan Petre

Improvements by Andrew Rowls Andrew Rowls的改进

I have it bound like this: 我有这样的约束:

<input type="text" data-provide="datepicker" ng-model="obj.FirstDate" />

I get the values to inputs using ng-model . 我使用ng-model将值输入。

When I type the date into this field using keyboard it all works OK, but when I click on a field and select a date from the Datepicker: 当我使用键盘在该字段中输入日期时,一切正常,但是当我单击一个字段并从Datepicker中选择一个日期时:

  • the model doesn't get updated, 该模型没有更新,
  • the field is not treated as dirty (no ng-dirty class). 该字段不会被视为脏字段(无ng-dirty类)。

Is there a way to tell Angular to update value of obj.FirstDate when using the Datepicker? 使用obj.FirstDate时是否有办法告诉Angular更新obj.FirstDate值? For example to attach it to an event? 例如将其附加到事件? Or any other way that this would work? 或其他任何可行的方法?

I have a few of these fields so I don't want to write the script which attaches to a field using its id . 我有几个这样的字段,所以我不想编写使用id附加到字段的脚本。 Any help appreciated. 任何帮助表示赞赏。

After much time of struggling with this I was forced to use below code to fix every of my datepicker instances at once: 经过很多时间的努力之后,我被迫使用下面的代码一次修复每个datepicker实例:

function fixDatepickerTriggerChange() {
    $(document).ready(function () {
        $('[data-provide="datepicker"]').datepicker().on('changeDate', function (e) {
            angular.element($(this)).triggerHandler('input');
        });
    });
}

And run it from within the angular.controller . 并从angular.controller内部运行它。

Based on this answer: https://stackoverflow.com/a/23850753/1813219 . 基于以下答案: https : //stackoverflow.com/a/23850753/1813219

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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