AngularJS filter by select with empty options

I am trying to build a filtering system for a table in AngularJS.

I have a list of products which are each tied to a system. I want to be able to have all systems listed in a select, and filter the products by the selected system.

This works great except for choosing the empty select option, which filters out all systems.

Here is my example code:

The controller:

function Controller($scope) {
$scope.products = [
    {id: 1, system_id: 1, name: 'Product 1'},
    {id: 2, system_id: 1, name: 'Product 2'},
    {id: 3, system_id: 2, name: 'Product 3'}

$ = [
    {id: 1, name: 'System 1'},
    {id: 2, name: 'System 2'},
    {id: 3, name: 'System 3'}


and the HTML:

<div ng-app ng-controller="Controller">

System: <select ng-model="filter.system_id" ng-options=" as for system in systems">
            <option value="">All Systems</option>

<li ng-repeat="product in products | filter: filter : true">
    {{ }}

Here is a working example:

My end goal is to have multiple filters, where filter is the filtering object with each property tied to a separate select.

I am guessing that the problem is that the empty option is an empty string which is causing false on equality because of the strict option, but is there a way to make it behave correctly in this siutation?


var app = angular.module('myApp',['myFilters']);

angular.module('myFilters', []).
filter('customFilter', function () {
    return function (products,filter) {
        var results = [];
        if(!filter.system_id) {
            return products;
        angular.forEach(products, function(product) {
            if(product.system_id === filter.system_id) {
        return results;

   <li ng-repeat="product in products | customFilter: filter">
        {{ }}

we can use a custom filter to accomplish this

see in action


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.