bootstrap-datepicker does not update AngularJS model when picking date

放肆的年华 提交于 2019-12-24 00:38:11

问题


I am using AngularJS with this bootstrap-datepicker plugin:

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

Copyright 2012 Stefan Petre

Improvements by 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.

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:

  • the model doesn't get updated,
  • the field is not treated as dirty (no ng-dirty class).

Is there a way to tell Angular to update value of obj.FirstDate when using the Datepicker? 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. Any help appreciated.


回答1:


After much time of struggling with this I was forced to use below code to fix every of my datepicker instances at once:

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.

Based on this answer: https://stackoverflow.com/a/23850753/1813219.



来源:https://stackoverflow.com/questions/41704902/bootstrap-datepicker-does-not-update-angularjs-model-when-picking-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!