1

I am trying to use a filter that I have written in my controller to narrow down a select list using ng-options. The filter logic is working, but the results aren't getting displayed.

Here's the relevant part of my controller:

        $scope.ESGFilter = function() {
            var esg;
            var filteredESGs = [];

            // LOGIC GOES HERE TO POPULATE THE filteredESGs ARRAY

            console.log(filteredESGs);
            return filteredESGs;
        }]);

Here's the HTML:

    <select class="input-block-level" id="inputESG" name="inputStoch"
         ng-model="Run.ESG" 
         ng-options="econ.Id as econ.Name for econ in econData | filter:ESGFilter" size="5">
    </select>

I suspect the issue has to be with "filter:ESGFilter" in the HTML... I can't figure out why the array that I return from the function isn't being displayed. The function in the controller is definitely being called and the logic is working.

For reference, the console.log outputs: [Object, Object] -- when I expand the properties of these objects, I confirm that they are the ones that I want. The select list, however, displays all four objects (I'm filtering down from an array of four).

1 Answer 1

2

Your filter needs to return a true/false statement. Write your filter like this:

$scope.ESGFilter = function(item) {
  // if you want to keep the item return true, else false
};

This works because the ngOptions takes each item in your array and passes it to your filter function. Your example is not working because it's returning an empty array which will always resolve to a truthy value, and all objects will be accepted and not filtered.

Here's a plunk:

http://plnkr.co/edit/RfKMcXDKy1m1EGyoEpS7?p=preview

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

1 Comment

You are a God among men, thank you very much! I suppose I should have figured that out by realizing why my console.log() statement was executing so many times... I was using an angular.forEach in my logic, so I unknowlingly had a nested for loop (since the items are all passed in from ng-options anyway).

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.