1

I have two arrays, one contains the Min-Prices and the other contains the Max-Prices, and if the user chooses a min-price of say "8000" from the Min-price dropdown, I want to show only the values that is greater than "8000" in the Max-Price dropdown.

How can I approach that with Angular.js?

Here's a JSFiddle ... http://jsfiddle.net/sheko_elanteko/fxkSz/10/

<div ng-app="app">
<div ng-controller="MainController">
    <form action="#">
        <label>Min Price</label>
        <select name="min-price">
            <option ng-repeat="num in minPrice" value="{{ num }}">{{ num }}</option>
        </select>

        <br>

        <label>Max Price</label>
        <select name="max-price">
            <option ng-repeat="num in maxPrice" value="{{ num }}">{{ num }}</option>
        </select>
    </form>
</div>

and the js ...

var app = angular.module('app', []);

app.controller('MainController', function($scope){

$scope.minPrice = [200, 400, 600, 800, 1000];
$scope.maxPrice = [200, 400, 600, 800, 1000];

});
4
  • 1
    I would start by creating some test data and test code on jsFiddle so that you can play around and have something to reference when you have a specific question. Commented May 12, 2014 at 11:45
  • I just started learning Angular, and I can't seem to get my head around it just yet. So, I have no idea how to approach this issue. Commented May 12, 2014 at 11:47
  • Ok, but you can create test data and HTML dropdowns and any required CSS, you know that right? And add the angular library and make an attempt, all on jsFiddle. Commented May 12, 2014 at 11:49
  • 1
    I made a fiddle and edited the post. Commented May 12, 2014 at 12:21

1 Answer 1

1

I made this fiddle that solves your problem. Its a pretty simple use of ngRepeat & $scope, and that is all.

Adding the code, as suggested

The JS part is;

var app = angular.module('App', [])

.controller('MainCtrl', ['$scope', function ($scope) {
    $scope.mins = [100, 200, 300, 400];
    $scope.maxs = [200, 300, 400, 500];

    $scope.getMxRange = function () {
        var arr = [];
        if ($scope.minVal) {
            angular.forEach($scope.maxs, function (maxVal) {
                if ($scope.minVal < maxVal) {
                    this.push(maxVal);
                }
            }, arr);
        }
        else {
            arr = $scope.maxs;
        }
        return arr;
    };
}]);

And ofcourse, the HTML looks something like;

<div ng-app="App" ng-controller="MainCtrl">
    <select ng-model="minVal">
        <option ng-repeat="min in mins" value="{{min}}">{{min}}</option>
    </select>
    <select>
        <option ng-repeat="max in getMxRange()" value="{{max}}">{{max}}</option>
    </select>
</div>

I believe the code is pretty self explanatory. The ngModel minVal contains the selected minimum value. and the getMxRange method returns all values in maxs which are greater than the value of the selected option in the range of minimum values.

Sign up to request clarification or add additional context in comments.

1 Comment

Please don't rely on links. Post the code in your answer, along with explanations as to how it works.

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.