NG-Repeat使用过滤器与当前日期进行比较?

时间:2013-12-02 18:39:03

标签: javascript angularjs angularjs-ng-repeat

好的我有一个包含日期列表的对象,我正在遍历它:

<select ng-click="monthpicker()" >
<option class="animate-repeat" ng-repeat="returnpicker in monthpicker(alldatesdump)"      value="{{returnpicker.date}}">{{returnpicker.date  | date:'yyyy-MMM' }}</option> 
</select>

使用ng-repeat会返回以下内容:

<select ng-click="monthpicker()">
  <!-- ngRepeat: returnpicker in monthpicker(alldatesdump) -->
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
对象中每个月的

等等,这是因为它返回对象中每个月的名称和一年。 好的,我有两个问题: 1:如何过滤结果,以便它们只返回每个月名称和年份的一个示例? 2:我如何设置它,使其仅从当前月份返回? 我想仅使用AngularJS过滤器来实现这一点,但如果需要,我确实可以使用jquery! 的 * ** * ** * * 更新 * ** * ** * ** * **** 继承了我的JS文件中的当前范围项:

scope.monthpicker = function(alldatesdump){

var alldatesdump = booking.getalldates();
/*for (var date in alldatesdump){
    if (alldatesdump.hasOwnProperty(date)){
        console.log(date);
    }
}
for (var date in alldatesdump) {
   var obj = alldatesdump[date];
   for (var prop in obj) {
      // important check that this is objects own property 
      // not from prototype prop inherited
      if(obj.hasOwnProperty(prop)){
        console.log(prop + " = " + obj[prop]);
      }
   }
}*/
return alldatesdump;
}; 

1 个答案:

答案 0 :(得分:6)

以下是我如何使用过滤器仅显示过去两天的订单的示例

.filter('getOrders', function() {
  return function (orders) {

    var filtered_list = [];

    for (var i = 0; i < orders.length; i++) {

      var two_days_ago = new Date().getTime() - 2*24*60*60*1000;
      var last_modified = new Date(orders[i].last_modified).getTime();

      if (two_days_ago <= last_modified) {
        filtered_list.push(orders[i]);
      }
    }
    return filtered_list;
  }
});

DOM看起来像这样

<tr ng-repeat="order in orders|getOrders">

希望这个小提琴帮助JSFiddle