0

i have block with filters:

 <form action="">
                    <input type="checkbox" id="inp1">
                    <label for="inp1">All items</label>
                    <input type="checkbox"  id="inp2">
                    <label for="inp2">New today</label>
                    <input type="checkbox" id="inp3">
                    <label for="inp3">New this week</label>
                    <input type="checkbox" id="inp4">
                    <label for="inp4">Free delivery</label>
                </form>

Filters for "new today" and "New this week":

  $scope.newThisWeek = function(products){
                     $scope.time = (Date.now() - Date.parse(products.add_update_time));

                     if ($scope.time <= 604800000) {
                         return $scope.products;
                     } else {

                         return null;
                     }

                };

                $scope.newToday = function(products){
                       $scope.time = (Date.now() - Date.parse(products.add_update_time));

                       if ($scope.time <= 86400000) {
                           return $scope.products;
                       } else {

                           return null;
                       }
                };

I use filters as there:

<div class="catalog-item" ng-repeat="product in products |filter:newThisWeek|filter:newToday">
 <a href="{{product.external_url}}" class="item-title">{{ product.name }}</a>
</div>

How to activate the filtering function in the checkbox angular?

1 Answer 1

1

You need to fist bind the inputs in your html to the $scope, using ng-model attribute:

..
 <input type="checkbox" id="inp2" ng-model="newTodayActivated">
 <label for="inp2">New today</label>
..other inputs

By this you create a $scope.newTodayActivated property that will be updated (toggle true/false) on change of the checkbox state . Then you can use this value in the filter expression functions:

$scope.newToday = function(products){      
  //use $scope.newTodayActivated - as Boolean here in your logic
  if($scope.newTodayActivated) {
   //do stuff
  } else {
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.