angular-UI datepicker在控制器中应用日期过滤器

时间:2014-11-05 06:18:50

标签: angularjs angular-ui

我正在使用angular-ui的datepicker,我在我的应用程序的许多地方使用了这个datepicker,所以我必须在每个控制器中应用日期过滤器。为避免这种情况,我需要在基准日期选择器控制器中应用日期过滤器

喜欢$scope.formData.order_date = $filter('date')($scope.formData.order_date, 'dd-MM-yyyy');

基本的datepicker控制器:

controller('DatepickerDemoCtrl', [
    '$scope', function($scope) {
      $scope.today = function() {
        return $scope.dt = new Date();
      };
      $scope.today();
      $scope.showWeeks = true;
      $scope.toggleWeeks = function() {
        return $scope.showWeeks = !$scope.showWeeks;
      };
      $scope.clear = function() {
        return $scope.dt = null;
      };
      $scope.disabled = function(date, mode) {
        return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6);
      };
      $scope.toggleMin = function() {
        var _ref;
        return $scope.minDate = (_ref = $scope.minDate) != null ? _ref : {
          "null": new Date()
        };
      };
      $scope.toggleMin();
      $scope.open = function($event,opened) {
        $event.preventDefault();
        $event.stopPropagation();
        console.log($scope);

        $scope[opened] = true;
      };

      $scope.dateOptions = {
        'year-format': "'yy'",
        'starting-day': 1
      };
      $scope.formats = ['dd-MM-yyyy', 'yyyy/MM/dd', 'shortDate'];
      return $scope.format = $scope.formats[0];
    }
  ]) 

如何在datepicker控制器中添加常用日期过滤器?有可能吗?

1 个答案:

答案 0 :(得分:0)

如果它只是您想要的基本日期格式,您可以直接在datepicker标签本身上应用“format-day”,“format-month”和“format-year”元素。请参阅此前已回答的stackoverflow问题的解决方案:Angular bootstrap datepicker date format does not format ng-model value。有关详细信息,请参阅文档:http://angular-ui.github.io/bootstrap/