在范围滑块 angular js 上过滤 ng-repeat

Filter ng-repeat on range slider angular js

我在我的项目中使用了角度滑块。我想在范围滑块滑动时过滤 ng-repeat。我想过滤价格数据。我的代码 html:-

<range-slider min="slider.min" max="slider.max" step="slider.step" lower-value="slider.min" upper-value="slider.max"></range-slider>
 <div ng-repeat="data in data|filter:rangeFilter()">
    Property: {{data.name}}
    price: {{data.price}}
 </div>

控制器:-

$scope.data = [{

                 name : hoarding1,
                 price : 50000

                },
                {

                 name : hoarding2,
                 price : 50000

                },
                {

                 name : hoarding3,
                 price : 50000

                }
               ]

$scope.slider = {

    step : 10000,
    min : Math.min.apply(Math,$scope.data.map(function(item){return item.price;})),
    max : Math.max.apply(Math,$scope.data.map(function(item){return item.price;}))

}
$scope.rangeSlider = function (){

 return function( items, rangeInfo ) {
    var filtered = [];
    var min = parseInt(rangeInfo.min);
    var max = parseInt(rangeInfo.max);
    // If time is with the range
    angular.forEach(items, function(item) {
        if( item.price >= min && item.price <= max ) {
            filtered.push(item);
        }
    });
    return filtered;
};

这对我不起作用。 请帮助我:-(

如果您要实施自定义过滤器,您应该在此处查看信息 https://docs.angularjs.org/tutorial/step_09

创建自定义过滤器后,您应该像下面这样使用它:

<div ng-repeat="data in data | customFilterName">

注意: 我建议您不要像 data 一样为每个数据实例使用名称。也许会更好 'item in data' 或那样。

我认为你需要一个自定义过滤器来过滤特定价格并显示在价格上具有过滤器边界的特定项目,请参阅下面的代码

控制器:

$scope.rangeInfo = {
    min : Math.min.apply(Math,$scope.data.map(function(item){return item.price;})),
    max : Math.max.apply(Math,$scope.data.map(function(item){return item.price;}))
  }

模板:

<div ng-repeat="data in data | priceRange:rangeInfo">
    Property: {{data.name}}
    price: {{data.price}}

 </div>

过滤器

可以使用数组的forEach方法或者数组的filter方法(ES5)

app.filter('priceRange',function(){
  return function(arrayItems,rangeInfo){
     return arrayItems.filter(function(item){
       console.log(item.price);
       return (item.price > rangeInfo.min && item.price < rangeInfo.max);
     }); 
    } 
  });

ES2015 (ES6) 使用箭头函数的过滤方法

   app.filter('priceRange',function(){

      return function(arrayItems,rangeInfo){

         return arrayItems.filter((item) => (item.price > rangeInfo.min && item.price < rangeInfo.max));
      }

});

https://plnkr.co/edit/JQgcEsW4NIaAwDVAcfy0?p=preview