Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make format filter for two way binding using AngularJS

On the page I have <input type="text" /> to which user will enter date in format 31/12/2013. This input field should be binded to $scope.startDate field. But on $scope.startDate I have to store date in following format "/Date(1385063675188)/" (WCF REST service date format).

Question: How to make two way binding between html input and AngularJS model where date on both will be in different format (dd/MM/yyyy and "/Date(1385063675188)/").

like image 261
MaciejLisCK Avatar asked Mar 21 '23 13:03

MaciejLisCK


1 Answers

I'd use a directive for this. Like so:

directives.directive('dateConverter', ['$filter', function($filter) {
    return {
      require: 'ngModel',
      link: function(scope, element, attrs, ngModelController) {
        ngModelController.$parsers.push(function(date) {
          // Do to model conversion
        });

        ngModelController.$formatters.push(function(date) {
          // Do to view conversion, possibly using $filter('date')
        });
      }
    };
  }]);
like image 60
Dormouse Avatar answered Apr 12 '23 13:04

Dormouse