0

I am new to AngularJS & I am using angular-slider for specifying a price range. I want to display the only elements specified by the range.

Here's the AngularJS Code-

var app = angular.module('myApp', ['rzModule']);
app.controller('SampleController', function($scope, $http) {
    $scope.elements = [1530,2100,2780,3323,3420,4680,5020,5300,5402];
});

app.controller('RangeController', RangeController);
function RangeController() {
    var vm = this;
    vm.changeListener = function() {
        minPrice = vm.slider.minValue;
        maxPrice = vm.slider.maxValue;
        console.log(minPrice + " " +maxPrice);
    };

    vm.slider = {
        minValue: 1500,
        maxValue: 5500,
        options: {
            floor: 1500,
            ceil: 5500,
            step: 500,
            translate: function(value) {
                return '₹' + value;
            },
            onChange:vm.changeListener
        }
    }   
}   

HTML-

<body ng-app="myApp">
<div ng-controller="RangeController as vm">
    <rzslider rz-slider-model="vm.slider.minValue" rz-slider-high="vm.slider.maxValue" rz-slider-options="vm.slider.options"></rzslider>
</div>
<div ng-controller="SampleController">
    <div ng-repeat="x in elements">
        {{x}}
    </div>
</div>

I think it can be done using filters but I am not sure. Any Solutions?

1 Answer 1

1

You can write a Custom AngularJS filter for this.

angular.module('myModule', [])
   .filter('inRange', function() {
        return function(array, min, max) {
            array = array.filter(function(element) {
                 return element >= min && element <= max;
            });
         };
    });
});
<div ng-repeat="x in elements | inRange:vm.slider.minValue:vm.slider.maxValue">
Sign up to request clarification or add additional context in comments.

3 Comments

I tried but it didn't work. Probably due to controllers being different? I only defined the filter in 'myApp' instead of creating a new one.
Ah yes -- sorry. I just noticed elements and minValue / maxValue are in different controllers. Try moving elements into your other controller or find a way to share the data between controllers if you need to (perhaps through a service).
Thanks. I'll try angularjs service.

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.