首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用angularjs从今天的日期获取列表的长度?

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

Stack Overflow用户
提问于 2014-07-21 09:10:57
回答 1查看 44关注 0票数 0

我正在使用angularjs构建一个应用程序。我使用mysql作为数据库。如何获取仅在当天更新的列表长度。?

为了从整个列表中获取长度,我使用了如下内容

代码语言:javascript
运行
复制
 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;
     };
 });

但我只想从今天的日期获取数据。“tripsheet”是我想要从中获取数据的列表。

EN

回答 1

Stack Overflow用户

发布于 2014-07-21 09:47:48

您需要使用成功回调或使用then回调访问promise,从而访问$http数据。

代码语言:javascript
运行
复制
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中,您可以执行以下操作:

代码语言:javascript
运行
复制
<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>

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

代码语言:javascript
运行
复制
var today=new date();
$scope.activeTrips= $scope.tripsheets.filter(function(trip){
       return new Date( trip.date) >= today;
 })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24856461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档