如何使用angularjs从今天的日期获得列表的长度?

时间:2014-07-21 01:10:57

标签: angularjs

我正在使用angularjs构建应用程序。我使用mysql作为数据库。 如何获得列表的长度,仅在今天的日期更新。?

为了从整个列表中获取长度,我使用了类似的东西

 app.factory('tripsheetFactory', function ($http) {
     return {
         getTripsheets: function () {
             return $http.get(urlt + '/all');
         },
         addTripsheet: function (tripsheet) {
             return $http.post(urlt, tripsheet);
         },
         deleteTripsheet: function (tripsheet) {
             return $http.delete(urlt + '?id=' + tripsheet.id);
         },
         updateTripsheet: function (tripsheet) {
             return $http.put(urlt + '?id=' + tripsheet.id, tripsheet);
         }
     };
 });

 app.controller('IndexCtrl', function ($scope, $filter, tripsheetFactory) {
     $scope.getTotalTripsheets = function () {
         return $scope.tripsheets.length;
     };
 });

但我想仅从今天的日期获取数据。 " tripsheets"是我想从

获取数据的列表

1 个答案:

答案 0 :(得分:0)

您需要使用成功回调来访问$http数据,或者因为它是使用then回调的承诺。

app.controller('IndexCtrl', function ($scope, $filter, tripsheetFactory) {    
    /* call the method in factory */
    tripsheetFactory.getTripsheets().success( serverResponse){
        /* set scope var in ajax success callback */
        $scope.tripsheets= serverResponse;   
    });
});

现在在您的HTML中,您可以执行以下操作:

<span>Total trips : {{ tripsheets.length}} </span>

<ul>
     <li ng-repeat="trip in tripsheets">
        Driver {{trip.driver}} drove {{trip.distance}} on {{trip.date | date:'yyyy-MM-dd'}}
     </li>
</ul>

有许多方法可以过滤数据。一个简单的控制器过滤器,例如:

var today=new date();
$scope.activeTrips= $scope.tripsheets.filter(function(trip){
       return new Date( trip.date) >= today;
 })