0

I have a jquery ui datepicker on angular app,

It is defined like this:

app.directive('dwdatepicker', function() {
    return {
        restrict: 'A',
        require : 'ngModel',
        link : function (scope, element, attrs, ngModelCtrl) {
            $(function(){
                element.datepicker({
                    onSelect:function (date) {
                        scope.$apply(function () {
                            ngModelCtrl.$setViewValue(date);
                        });
                    },
                    dateFormat: 'dd/mm/yy',
                    changeYear: true,
                    yearRange: "-50:+0"
                });
            });
        }
    }
});

Now I want to store the value in $modelValue on yyyy-MM-dd format, and to show the value to the user on dd/mm/yy format.

How can I make it?

How to combine the capabilities of jquery ui datepicker with the capabilities of angular?

Thank you.

1 Answer 1

1

In the onSelect function you could do either of the following:

onSelect:function (date) {
   scope.$apply(function () {
      var isoDate = $.datepicker.formatDate('yy-mm-dd', $.datepicker.parseDate('dd/mm/yy', date));
      ngModelCtrl.$setViewValue(isoDate);
   });
}

Or

onSelect:function (date, ) {
   scope.$apply(function () {
      var isoDate = $.datepicker.formatDate('yy-mm-dd',$(this).datepicker('getDate'));
      ngModelCtrl.$setViewValue(isoDate);
   });
}

Haven't used jQuery in a while so syntax could be wrong.

1
  • I thought that the method $setViewValue update $viewValue (not $modelValue), does not it?
    – banana
    Commented Feb 6, 2017 at 10:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.