【问题标题】:NG-Repeat compare to current date using filter?NG-Repeat 使用过滤器与当前日期比较?
【发布时间】:2014-07-29 05:03:16
【问题描述】:

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

<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;
}; 

【问题讨论】:

    标签: javascript angularjs angularjs-ng-repeat


    【解决方案1】:

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

    .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

    【讨论】:

    • 谢谢你,这看起来很有帮助,如果你不介意我问,你的 dom 绑定是什么样的?
    • @vimes1984 我将它添加到答案中。返回函数中的“orders”与orders|相同被过滤。
    • 好答案。另请参阅这个,它非常相似,但允许您设置要查看的时间段。 stackoverflow.com/questions/24642449/…
    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2015-08-02
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多