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

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -