2015-08-23 52 views
0

我正在试图制作一个过滤器,它只会显示一月份某月的json条目。但是,我遇到了麻烦。我尝试设置1月和2月的变量,然后将输入设置为大于1月的第一个日期并且小于2月的第一个日期,但这种方法无效。任何帮助将不胜感激。angular.js按月过滤

这里是我的html:

<div ng-app="myApp" ng-controller="Catalog"> 
<div class="bg" ng-repeat="book in books"> 
    <div ng-show=" book.doc.date | mydate" > 
    <div class="date">{{book.doc.date}}</div> 
    <div class="title">{{book.doc.title}}</div> 
    <div class="quote">{{book.doc.quote}}</div> 
    <div class="attribution">-{{book.doc.attribution}}</div> 
    <div class="textt">{{book.doc.text}}</div> 
    <div style="height:10px"></div>  
    </div> 
    </div> 

    <div class="bg" ng-repeat="book in books "> 
    <div ng-click="showInfo = !showInfo" ng-show=" book.doc.date | January"> 
    <div class="date">{{book.doc.date}}</div> 
    <div class="title">{{book.doc.title}}</div> 
     <div class="quote" ng-show="showInfo">{{book.doc.quote}}</div> 
      <div class="attribution" ng-show="showInfo">- {{book.doc.attribution}}</div> 
       <div class="textt" ng-show="showInfo">{{book.doc.text}}  </div>  
<div style="height:10px"></div>  
</div> 
</div> 
</div> 

和我的JS:

var myApplication = angular.module('myApp', ['ngColorThis']); 
myApplication.controller("Catalog", function ($scope) { 
$scope.books = books; 
$scope.showInfo = false; 
}) 

.filter('mydate', function() { 
return function(input) { 
var today = new Date(); 
var dd = today.getDate(); 
var mm = today.getMonth() + 1; //January is 0, so always add + 1 
var yyyy = today.getFullYear(); 
if (dd < 10) { 
    dd = '0' + dd 
} 
if (mm < 10) { 
    mm = '0' + mm 
} 
today = mm + '/' + dd; 
return (input == today) 
} 
}) 

.filter('past', function() { 
return function(input) { 
var today = new Date(); 
var dd = today.getDate(); 
var mm = today.getMonth() + 1; //January is 0, so always add + 1 
var yyyy = today.getFullYear(); 
if (dd < 10) { 
    dd = '0' + dd 
} 
if (mm < 10) { 
    mm = '0' + mm 
} 
today = mm + '/' + dd; 
return (input) 
} 
}) 

.filter('January', function() { 
return function(input) { 
var today = new Date(); 
var dd = today.getDate(); 
var mm = today.getMonth() + 1; //January is 0, so always add + 1 
var yyyy = today.getFullYear(); 
if (dd < 10) { 
    dd = '0' + dd 
} 
if (mm < 10) { 
    mm = '0' + mm 
} 
today = mm + '/' + dd; 
var jan = 00 + "/" +00; 
var feb = 01 + "/" + 00; 
return (input > jan && input < feb) 
} 
}); 

而且,这里是我的项目的完整plunker

回答

1

,让您的January过滤器工作的最简单方法如下:

.filter('January', function() { 
    return function(input) { 
    return input.slice(0,2) === '01'; 
    } 
}); 

正如你所看到的,所有需要的是前两个数字的简单对比。

当然你也可以简单的内联表达式:

ng-show="book.doc.date.indexOf('01') == 0" 

更妙的是,使用ng-if代替ng-show,或显示所有想要的物品之前滤镜阵列。

1

你应该在你过滤器表达式中使用filter

<div ng-show=" book.doc.date | filter: mydate" > 

<div ng-click="showInfo = !showInfo" ng-show=" book.doc.date | filter: January">