Jquery datepicker selected date is not considered in ng-change - AngularJS

家住魔仙堡 提交于 2019-12-01 22:57:54
bdavidxyz

Such complex UI components need to be wrapped into a directive to maintain a model data binding. For the jQuery datepicker, you can check this question, or google around "jquery datepicker directive".

Kirubanandan Kanagaraj
<input type="text" id="date1" datepicker1 ng-model="gettingStartDate" />
var app = angular.module('reports', [])
.directive("datepicker1", function () {
    return {
        restrict: "A",
        link: function ($scope, $element, $attr, $controller) {
            $element.datepicker({
                changeMonth: false,
                showOtherMonths: true,
                numberOfMonths: 2,
                dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
                onSelect: function (date) {
                    $scope.gettingStartDate = date;
                    $scope.$apply();
                    $scope.fromDateSend(date); // function call
                },
                onClose: function (selectedDate) {
                    $("#date2").datepicker("option", "minDate", selectedDate);
                }
            });
        }
    };
});

You should change the entire approach. Using jQueryUI datepicker directive:

angular
    .module('App',['ui.date'])
    .directive('customDatepicker',function($compile){
        return {
            replace:true,
            templateUrl:'custom-datepicker.html',
            scope: {
                ngModel: '=',
                dateOptions: '='
            },
            link: function($scope, $element, $attrs, $controller){
                var $button = $element.find('button');
                var $input = $element.find('input');
                $button.on('click',function(){
                    if($input.is(':focus')){
                        $input.trigger('blur');
                    } else {
                        $input.trigger('focus');
                    }
                });
            }    
        };
    })

See demo

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