使用angular.js过滤器逐月过滤日期

时间:2013-12-29 17:48:13

标签: angularjs date filtering

我可以使用angularjs的filter语句按日期过滤数据,但现在我只需按月和年过滤它,有一个简单的方法吗?

这是我的代码:

app.js

$scope.expenses = [{amount:100.00, date:"2013-12-20"}, {amount:100.00, date:"2013-12-20"}];

的index.html

<input ng-model="expenseDate" type="date" value="dd-mm-yyyy" ng-required="true">            
<li ng-repeat="expense in expenses | filter:{date:expenseDate}">
    <span>{{expense.date)}}, {{expense.amount}} </span>
</li>

被修改

我不能做这样的事吗?

<li ng-repeat="expense in MyOwnFilter(expenses) | filter:{date:expenseDate}">
    <span>{{expense.date)}}, {{expense.amount}} </span>
</li>

或者

<li ng-repeat="expense in MyOwnFunction() | filter:{date:expenseDate}">
    <span>{{expense.date)}}, {{expense.amount}} </span>
</li>

2 个答案:

答案 0 :(得分:0)

虽然有很多方法可以做到这一点而不涉及改变数据,但最简单/最干净的事情可能是使用三个不同的值和输入。类似的东西:

$scope.expenses.map(function (expense) {
    var date = expense.date.split('-');

    exepnse.day = date[0];
    expense.month = date[1];
    expense.year = date[2];

    return expense;
});

然后只需添加日期,年份和日期的输入元素,并在ng-repeat上使用多个过滤器。

答案 1 :(得分:0)

搞定了

的index.html

<input ng-model="expenseDate" type="date" value="dd-mm-yyyy" ng-required="true"> 
<ul class="row">
    <li ng-repeat="exp in MyOwnFilterFunction()">
        <span>{{CategoryName(exp.category_id)}}, {{exp.amount}} </span>
    </li>
</ul>

app.js

$scope.expenses = [{amount:100.00, date:"2013-12-20"}, {amount:100.00, date:"2013-12-20"}];

$scope.MyOwnFilterFunction = function() {
    var data = []
    *** FILTER $escope.expenses USING $scope.expenseDate INTO data ***
    return data;
};