How to filter a select with ng-options in Angular?


I've written the following proof-of-concept of an Angular app that allows a person to vote for President of the US.

<!DOCTYPE html>
<html ng-app="ElectionApp"">
    <script src=""></script>
        var ElectionApp = angular.module("ElectionApp", []);
        var ElectionController = ElectionApp.controller("ElectionController", [function () {
            // Pretend that this data came from an outside data-source.
            this.candidates = {
                "14837ac3-5c45-4e07-8f12-5771f417ca4c": {
                    name: "Alice",
                    gender: "female"
                },"333eb217-c8d1-4b94-91a4-22a3770bbb22": {
                    name: "Bob",
                    gender: "male"
   = function () {
                // TODO: Record the user's vote.
<body ng-controller="ElectionController as ctrl">
    Who would you like to elect President? <br />
        ng-options="person as for (id, person) in ctrl.candidates | filter{gender:'female'}">
    <input type="button" value="Vote!" ng-submit=";" />

    <h2>Candidate Profiles</h2>    
    <div ng-repeat="candidate in ctrl.candidates">
        {{}}, {{candidate.gender}}

My voting app displays a list of the candidates names along with a profile for each candidate, which in this case, consists of the candidate's name and gender.

For the purpose of this question, please pretend that the roster of candidates to choose from is fetched from a remote data source, but will follow the same format as the example.

Suppose that shortly before the election is to be held, a constitutional amendment is passed mandating that the next US President must be female. Suppose that the data source cannot be updated in time for the election, and the boss said to me, "I've heard that with Angular, you can arbitrarily choose which items from the data source appear on the form using a filter. Make it happen!"

Following along with some examples I've seen on-line, I've written the above code, but it no longer displays any candidate. What did I do wrong?

How can I filter the options in a select list using an Angular filter?

Problem courtesy of: Daniel Allen Langdon


filter can only operate on arrays.

But you can create a custom filter:

ElectionApp.filter('females', [ function() {
    return function (object) {
        var array = [];
        angular.forEach(object, function (person) {
            if (person.gender == 'female')
        return array;

and then write

ng-options="name as for (id, person) in ctrl.candidates | females">
Solution courtesy of: wero


a bit late maybe, but for others looking for info I'm using this.

$scope.deliveryAddresses = data; 

Where data is complex Json id, name and city received from webapi in my Angular controller by $http.get

I'm using it in my Html page with the following snippet

ng-options=" for address in deliveryAddresses | filter:{name:search} track by

where search is a reference to a textbox. Simple and efficient.

Hope it helps someone.

Discussion courtesy of: Han

You just forgot ":" after filter keyword.

    ng-options="person as for person in ctrl.candidates | filter:{gender:'female'}">
Discussion courtesy of: Benoit Charpié-Pruvost

This recipe can be found in it's original form on Stack Over Flow.