Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an AngularJS application. I´m using a JQuery-ui datepicker. Everything is working fine, but the locale.

This is my directive:

.directive('customDatepicker', function($filter, DateService, LocationService, PriceService) {
    return {
        restrict: 'A',
        require : 'ngModel',
        link : function (scope, element, attrs, ngModelCtrl) {

            /* CODE WITH PROBLEM BEGIN */
            console.log("Setting calendar directive");
            var locale = LocationService.getLangKey();
            if (locale == 'en') {
                locale = '';
            }
            $("#checkinDate").datepicker("option", $.datepicker.regional[ locale ] );
            $("#checkoutDate").datepicker("option", $.datepicker.regional[ locale ] );      
            /* CODE WITH PROBLEM END */

            $(function() {
                element.datepicker({
                    minDate: 2,
                    dateFormat:'dd-M-yy',
                    maxDate: '+1y', 
                    showAnim: 'slideDown',
                    onSelect:function (date) {
                        scope.$apply(function () {
                            ngModelCtrl.$setViewValue(date);

                            validateDates();  
                            formatDates();           

                            function validateDates() {
                                console.log("customDatepicker.validateDates()");

                                // if arrivalDate is empty. Set.
                                if(scope.searching.arrivalDate == undefined || 
                                   scope.searching.arrivalDate < scope.searching.departureDate) {
                                    var arrivalDate = new Date(scope.searching.departureDate);
                                    arrivalDate.setDate(arrivalDate.getDate() + 1);        
                                    scope.searching.arrivalDate = arrivalDate;                          
                                    $("#checkoutDate").datepicker("option","minDate", arrivalDate);                                     
                                }
                            } 
                            function formatDates() {
                                if (scope.searching.departureDate != undefined) {
                                    var departureDateAux = new Date(scope.searching.departureDate);
                                    scope.searching.departureDate = $filter('date')(departureDateAux,'dd-MMM-yyyy');
                                }
                                if (scope.searching.arrivalDate != undefined) {
                                    var arrivalDateAux = new Date(scope.searching.arrivalDate);
                                    scope.searching.arrivalDate = $filter('date')(arrivalDateAux,'dd-MMM-yyyy');  
                                }                       
                            }                                                   
                        });
                    }
                });
            });                     
        }
    };
})

I´m trying to add locale functionality with the code between /* CODE WITH PROBLEM BEGIN / and / CODE WITH PROBLEM END */ The locale is also working but when the code is executed the calendar is automatically shown in the view, I mean, without clicking in the input field.

I attach a snapshot.Calendar displayed by default

share|improve this question

1 Answer 1

I fixed it replacing the hot code with:

var locale = LocationService.getLangKey();
if (locale == 'en') {
   locale = '';
}
element.datepicker("option", $.datepicker.regional[ locale ] );
$("#ui-datepicker-div").hide();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.