0

I have two array shaped like this:

$scope.values_array = ['string1', 'string2', 'string3', 'string4'];
$scope.skip_array = ['string2', 'string3'];

and I try to use ng-options like this:

<select ng-options="value as value for value in values_array && skip_array.indexOf(value) === -1"></select>

This doesn't produce an error but nothing shows up in the dropdown anymore. Without the "&& skip_array.indexOf(value)" it shows all the options in values_array.

4 Answers 4

10

html:

<select ng-model="selectedValue" ng-options="value as value for value in values_array | filter:skipValues"></select>

controller:

$scope.skipValues = function(value, index, array) {
    return $scope.skip_array.indexOf(value) === -1;
};
$scope.values_array = ['string1', 'string2', 'string3', 'string4'];
$scope.skip_array = ['string2', 'string3'];

jsfiddle

UDPATE:

If you want to pass an extra parameter to the filter function

html:

<select ng-model="selectedValue" ng-options="value as value for value in values_array | filter:skipValues(1)"></select>

controller:

$scope.skipValues = function(anInt) {
    return function(value, index, array) {
        return $scope.skip_array.indexOf(value) === -1 && anInt > 0;
    }
};
$scope.values_array = ['string1', 'string2', 'string3', 'string4'];
$scope.skip_array = ['string2', 'string3'];

jsfiddle

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

3 Comments

If I would want to add an extra parameter, in my case I want it to be able to skip it based on a passed int, how can I add this parameter to the function? (calling the function with 1 parameter and adding it to the function doesn't work)
@schuimer I have updated my post with a solution for your question.
Works like a charm! Thank you very much!
1

How about using a filter? Like

    <select ng-options="value as value for value in (values_array | yourFilter)"></select>

yourFilter might be something in the lines of this answer:

.filter('inArray', function($filter){
    return function(list, arrayFilter, element){
        if(arrayFilter){
            return $filter("filter")(list, function(listItem){
                return arrayFilter.indexOf(listItem[element]) != -1;
            }); 
        }
    };
});

Comments

1

Just use a function that will return the filtered values. You may also use a separate filter but a function should be much easier in this case

 <select name="mySelect" id="mySelect" ng-model="mySelectmodel" ng-options="value as value for value in filteredValues(values_array)">

$scope.filteredValues = function () {
    return $scope.values_array.filter(function (val) {
        return $scope.skip_array.indexOf(val) === -1;
        });
    };

Here is the full working example

FULL EXAMPLE

Comments

0

Html

<select ng-modal="yourmodalname" ng-options="x.id as x.name for x in arraylistofvalue | filter : selectedvalues"></select

angular controller

 $scope.selectedvalues = function(value , index , array ){
     if (value.id == 1){
            return $scope.arraylistofvalue.indexOf(value);
         }

      }

array list

$scope.arraylistofvalue = [{id : 1 ,name : 'value1'}{id : 2 ,name : 'value2'}]

Comments

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.