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

心已入冬 提交于 2019-12-03 04:45:43

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')
                array.push(person);
        });
        return array;
    };
}]);

and then write

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

You just forgot ":" after filter keyword.

<select 
    ng-model="ctrl.selection"
    ng-options="person as person.name for person in ctrl.candidates | filter:{gender:'female'}">
</select>

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="address.name for address in deliveryAddresses | filter:{name:search} track by address.id

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

Hope it helps someone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!