我有一個ng-repeat數組。 每個對象都有一個名爲"checkdate"
的屬性,它只是一個"New Date()"
。如何使用angularjs在ng-repeat中通過屬性'today'進行過濾?
我希望ng-repeat
只顯示今天創建的對象。我怎樣才能做到這一點?
感謝
我有一個ng-repeat數組。 每個對象都有一個名爲"checkdate"
的屬性,它只是一個"New Date()"
。如何使用angularjs在ng-repeat中通過屬性'today'進行過濾?
我希望ng-repeat
只顯示今天創建的對象。我怎樣才能做到這一點?
感謝
$scope.list1 = [
{name: "item1", checkdate: '2016-03-04T09:25:57.882Z'},
{name: "item2", checkdate: '2016-03-05T09:25:57.882Z'},
{name: "item3", checkdate: '2016-03-06T09:25:57.882Z'}];
var curDate = new Date();
var y = curDate.getFullYear();
var m = curDate.getMonth() + 1;
if (m < 10) {
m = '0' + m;
}
var d = curDate.getDate();
if (d < 10) {
d = '0' + d;
}
$scope.curDate = y + '-' + m + '-' + d;
...
<div ng-repeat="item in list1 | filter:{checkdate: curDate}">
{{item.name}} - {{item.checkdate}}
</div>
也許我在描述這個時應該更加徹底。 創建新條目時,checkdate字段由「new Date()」填充。但它會以字符串形式保存在數據庫中;像這樣: 2016-03-04T09:25:57.882Z 所以這個例子不起作用。也許我應該創建一個函數,在日期之後剝離結果並僅比較結果? –
已更新的答案。 –
使用以下命令:
ng-repeat="obj in objects"
和內:
ng-show="obj.date === Date.now()"
並修改Date.now()
根據你的對象的格式日期格式。
像here
或者你也可以做到以下幾點:
ng-show="checkDate(obj.date)"
其中
checkdate(date){
var todaysDate = new Date();
//call setHours to take the time out of the comparison
if(date.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0));
{
return true
} else { return false}
}
我試過這樣的http://codepen.io/utrolig/pen/YqwKpW ,但它似乎並沒有工作。 –
如果您嘗試將其與其他值進行比較,過濾器將無法正常工作 – uksz
這裏是你可以做的改變一個字符串到日期並比較對象中的ng-repeat =「obj」ng-if =「Date.parse(obj.checkdate)> = TODAY」'如果這是你的問題。 – Tim