0

Im trying to complete this custom filter to filter a list of all "savings" created in the last 24 hours

Controller filter

    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;

    };
});

And here is how i call it from the view

<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>

Ive wrote a note where the unresolved variable is. How do i declare the "saving" object which is coming from the database. Without the filter it returns all results fine.

Controller Code

    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'
      }
    });
  }
]);
7
  • 1
    You're missing the two most important pieces of code.. include your controller code and your Savings factory code. Commented Jan 30, 2016 at 2:15
  • is that the correct code? Commented Jan 30, 2016 at 2:18
  • Shouldn't there be this line in your controller: $scope.savings = Savings; ? Commented Jan 30, 2016 at 2:19
  • 1
    There is no $scope.savings.. is there another controller? Commented Jan 30, 2016 at 2:21
  • 1
    Right because you don't have any savings property in your scope unless you declare it. You need to include your Savings service as well Commented Jan 30, 2016 at 2:31

1 Answer 1

1

Well it's good to see that $scope.savings has snuck in there. Try something like this:

Instead of $scope.savings = Savings; use:

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

If your api endpoint needs the savingId use:

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

This should work for you.

Sign up to request clarification or add additional context in comments.

4 Comments

That console.log is printing out all the Objects properly so the problem must be somewhere else. maybe i have the filter wrong. Im gonna post the controller to pastebin.Is this the correct way to add the filter? pastebin.com/t3nBmFPn
The filter is dependent on the data structure so I would need to see the response.
Chrome doesnt seem to let me copy and paste so heres a screenshot i.imgur.com/M1jMOe3.png
OK this question is answered now and you can mark it. Can you post that image on your other question and I'll update that answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.