merch89 merch89 - 1 year ago 92
AngularJS Question

Only display items below £10 with Angular Filter

I have a list of products generated from a third party JSON feed, the prices will vary depending on the current price from the 3rd party.

How can I only display prices on the page below for a certain value, such as below £10.00?

I assume AngularJS filters will come into play here but now sure how it works with an expression such as item.products_price, but even better if a slider or something is also possible. So products are displayed below based on the value range in a slider.

Here is my 'List' code:

<!DOCTYPE html>
<html ng-app="plunker">

<meta charset="utf-8" />
<title>AngularJS Plunker</title>
document.write('<base href="' + document.location + '" />');
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="" data-semver="1.4.9"></script>
<script src="app.js"></script>

<ion-content class="has-subheader">
<ion-item ng-repeat="item in products | filter:query" class="item-thumbnail-left item-text-wrap">
<img ng-src="img/{{item.products_image}}" alt="{{item.products_name}} Photo">


Lex Lex
Answer Source

What you want to do is write a custom filter. Here is one way you might structure the filter:

.filter('valueRange', function() {
    return function(productList, minValue, maxValue) {
        // make sure something was passed - you can decide if you want to use defaults
        if(!productList || !minValue || !maxValue) {
            return productList;

        var out = [];
        angular.forEach(productList, function(product) {
            if(product.products_price >= minValue && product.products_price <= maxValue) {
        return out;

Then you can use it in your ng-repeat as:

<ion-item ng-repeat="item in products | valueRange:minValue:maxValue" class="item-thumbnail-left item-text-wrap">

This part - valueRange:minValue:maxValue - will call the valueRange filter, pass in products as the first parameter, minValue as the second parameter and maxValue as the third parameter. You can get these values from your slider.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download