angularjs - Set modelValue as a different date format to the viewValue in Angular directive -
i have datepicker within form. set 1 format view apply different format model value (which sent api). in simple terms want user see 'dd/mm/yyyy' need date sent in iso format.
this directive:
app.directive('standarddatepicker', function($timeout) { return{ restrict: 'a', require : '^ngmodel', link: function(scope, element, attrs, ngmodel, ngmodelctrl){ element.datepicker({ format: "dd/mm/yyyy", autoclose: true, }).on('changedate', function(e) { ngmodel.$viewvalue = e.date; ngmodel.$render(); }); } } });
is there easy way achieve this?
you should use $formatters , $parsers
function standarddatepicker() { return { require: 'ngmodel', link: function ($scope, $elem, $attrs, $ctrl) { var ngmodelctrl = $ctrl; ngmodelctrl.$formatters.unshift(function (value) { return value.format('yyyy'); }); ngmodelctrl.$parsers.unshift(function (value) { return value.format('yyyy-mm-dd'); }); } } }
Comments
Post a Comment