【问题标题】:How to make format filter for two way binding using AngularJS如何使用AngularJS为两种方式绑定制作格式过滤器
【发布时间】:2013-11-21 20:34:54
【问题描述】:

在我有<input type="text" /> 的页面上,用户将以31/12/2013 格式输入日期。此输入字段应绑定到$scope.startDate 字段。但是在$scope.startDate 上,我必须以以下格式存储日期"/Date(1385063675188)/"(WCF REST 服务日期格式)。

问题:如何在 html 输入和 AngularJS 模型之间进行双向绑定,其中两者的日期格式不同(dd/MM/yyyy"/Date(1385063675188)/")。

【问题讨论】:

    标签: json wcf rest angularjs data-binding


    【解决方案1】:

    我会为此使用指令。像这样:

    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')
            });
          }
        };
      }]);
    

    【讨论】:

      【解决方案2】:

      您可以使用scope.$watch

      scope.$watch("model", function (newValue) {
          scope.formattedModel = convertToWcfFormat(newValue);
      });
      

      【讨论】:

        猜你喜欢
        • 2015-09-17
        • 2014-02-13
        • 2016-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多