Angular ng-repeat自定义过滤器提供未解决的变量

时间:2016-01-30 02:07:50

标签: angularjs

我正在尝试完成此自定义过滤器,以过滤过去24小时内创建的所有“节省”的列表

控制器过滤器

    angular.module('savings').filter('lessThan', function () {
    return function(savings, requirement) {

        var filterKey = Object.keys(requirement)[0];
        var filterVal = requirement[filterKey];

        var filtered = [];

        if(filterVal !== undefined && filterVal !== ''){
            angular.forEach(savings, function(saving) {

                var today = new Date();
                var date = new Date(saving.created.$date); <-- Unresolved variable $date
                alert(date);
                var diff = today - date;

                diff = diff / (1000*60*60);

                if(diff < filterVal) {
                    filtered.push(saving);
                }
            });
            return filtered;

        }

        return savings;

    };
});

以下是我从视图中调用它的方式

<div ng-repeat="saving in savings |  orderBy: '-votesreal' | limitTo:6 | lessThan: {'created.$date':24}" class="col-lg-2 no-padding-spotlight text-center">

        <div class=" thumbnail-spotlight thumbnail centred-image">
          <img src="{{saving.image}}" /><br>
          <a class="text-center" ng-href="/savings/{{saving._id}}" ng-bind="saving.title +' (&euro;'+  saving.price +' @ '+  saving.retailer+')'"></a>
        </div>

      </div>

我写了一个未解决变量的注释。如何声明来自数据库的“保存”对象。没有过滤器,它可以很好地返回所有结果。

控制器代码

    angular.module('savings').controller('SavingsController', ['$scope', '$timeout', '$stateParams', '$location', '$window', 'Authentication', 'Savings', 'FileUploader',
        function($scope, $timeout, $stateParams,  $location, $window, Authentication, Savings, FileUploader) {

            $scope.authentication = Authentication;
            $scope.user = Authentication.user;
            $scope.savingImageURL = '/modules/users/client/img/profile/saveme-placeholder.png';
            // $scope.user.imageURL  = '/modules/users/client/img/profile/saveme-placeholder.png';
            $scope.imageURL1 = '';
            $scope.hottestsorted = true;
            $scope.newestsorted = true;
            $scope.brandLogo = '/modules/users/client/img/profile/argos-logo.png';
            $scope.spotlightSort = Savings.votesreal;
             $scope.savings = Savings;
            //$scope.user.imageURL = '';
            $scope.submitFormSaving = function(isValid) {
                $scope.submitted = true;
            };

    }
]);

Client.service

    //Savings service used for communicating with the savings REST endpoints
angular.module('savings').factory('Savings', ['$resource',
  function ($resource) {
    return $resource('api/savings/:savingId', {
      savingId: '@_id'
    }, {
      update: {
        method: 'PUT'
      }
    });
  }
]);

1 个答案:

答案 0 :(得分:1)

很高兴看到$ scope.savings在那里偷偷摸摸。尝试这样的事情:

而不是$scope.savings = Savings;使用:

Savings.query({}, function(resp){
  console.log(resp);
  $scope.savings = resp;        
}); 

如果您的api端点需要使用savingId:

Savings.query({ savingId: [something] }, function(resp){
  console.log(resp);
  $scope.savings = resp;        
});

这应该适合你。