Angular JS按日期筛选不起作用

Angular JS filter by date not working

本文关键字:筛选 不起作用 日期 JS Angular      更新时间:2023-09-26

我正试图过滤表中的数据,使其只显示在某个日期输入的项目,但我似乎无法做到这一点。当我选择一个新日期时,我可以看到模型正在更新,但表中的数据不会改变。

HTML:

 <tr ng-repeat='file in files  | filter: date '>

日期选择器HTML:

<label>From:</label> 
  <p class="input-group" style="width:200px">
    <input type="text" class="form-control" uib-datepicker-popup = "{{format}}" ng-model="date" is-open="status.opened"
                                       min-date="minDate" max-date="maxDate" datepicker-options="dateOptions" date-disabled="disabled(date, mode)"
                                       ng-required="true" close-text="Close"/>
        <span class="input-group-btn">
          <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
        </span>
  </p>

控制器:

(function ()
 {
  'use strict';
 angular.module('aml-tech-dashboard.successful-emails').controller('datepickerController', function ($scope) {
$scope.date = '';
$scope.status = {
    opened: false
};
$scope.format = "yyyy-MM-dd"
$scope.minDate = new Date();
$scope.dateOptions = {
    //formatYear: 'yy',
    startingDay: 1
};
$scope.open = function ($event) {
    $scope.status.opened = true;
};
$scope.date = new Date();
});
})();

您应该使用对象进行筛选。

<tr ng-repeat='file in files  | filter: date '>
$scope.date = {date: '2016-08-01'}

每个"文件"都必须有字段"日期"

所以我解决了这个问题。文件采用JSON格式,日期选择器中的日期为date对象。

我在控制器中添加了一个监听器,它将新日期转换为JSON,这为我修复了它:

 $scope.$watch("date", function(newval, oldval) {
        $scope.dateString = $scope.date.toJSON();
    });

(并使用新的dateString值进行过滤)