2016-05-13 3 views
0

私はフィルタでブロックを持っている: "新今日" と "今週" のチェックボックスでフィルター機能を有効にするにはどうすればいいですか?

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

フィルタ:私のように、フィルタを使用し

$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; 
         } 
       }; 

が:

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

どのようにチェックボックスのフィルタリング機能を有効にしますか?

答えて

1

あなたはNG-モデルを使用して、$ス​​コープにあなたのhtmlでの入力をバインド拳する必要が属性:これにより

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

あなたが更新されます$scope.newTodayActivatedプロパティを作成します(真/偽切り替えます)チェックボックスの状態の変更に。次に、フィルタ式関数でこの値を使用できます。

$scope.newToday = function(products){  
    //use $scope.newTodayActivated - as Boolean here in your logic 
    if($scope.newTodayActivated) { 
    //do stuff 
    } else { 
    } 
} 
関連する問題