Search code examples
angularjsangular-filters

display array conditionally : Angular


I'm trying to filter my array : here is my fiddle : Demo.
there are two select list , here is the condition :

when top select list === 123        ====> bottom select list should show 001,002,003 

and

when top select list === 1234        ====> bottom select list should show 002,004,005

should i use something like this .slice(1, 3) ?
Many Thanks


Solution

  • Here is a fiddle that does what you want, where 1234 shows 002,004,005 just because it does. here

    var app = angular.module('myApp', []);
    app.controller('mainCtrl', function($scope){
    $scope.colors = [
        {name:'black', shade:'123'},
        {name:'white', shade:'1234'},
    
    ];
    var allRanges =  [
        {id:'001', number:'1'},
        {id:'002', number:'2'},
        {id:'003', number:'3'},
        {id:'004', number:'4'},
        {id:'005', number:'5'}
    ];
    
    $scope.range = [];
    
    
    
    $scope.check = function(){
        var filter;
        if($scope.color === "black"){
            filter = function(r){
                if(r.number < 4){
                    return true;
                }
            };
        } else if($scope.color === 'white'){
            filter = function(r){
                if(['2','4','5'].indexOf(r.number) >= 0){
                    return true;
                }
            };
        }
        $scope.range = allRanges.filter(filter);
    }
    

    });