Search code examples
javascriptangularjsng-options

ng-option filter not working angularjs


I'm trying display data from an array in a select using ng-options and I want filter it which is not working. I don't want to show object having billHeadShortForm=FEC & FDG

Here is my HTML

<select class="form-control" ng-init="getBillHeadCurrentProjWise()" ng-model="headID" ng-options="h.billHeadID as h.billHead for h in billHeadsProjWise | filter:h.billHeadShortForm!='FEC' | h.billHeadShortForm!='FDG'">
     <option value="">--Select Billing Head--</option>
</select>

Solution

  • Adjust the filter part of the ng-options to the following:

    ng-options="... | filter: {billHeadShortForm: '!FEC'} | filter: {billHeadShortForm: '!FDG'}"
    

    See fiddle

    Although, you might want to read filter documentation, and write a function to avoid piping two filters.

    Edit:

    The function could be something like that:

    $scope.filterBillHead = function (billHead) {
        // Exclude 'FEC' and 'FDG'
        // return true if billHeadShortForm is not in the array, false otherwise
        return ['FEC', 'FDG'].indexOf(billHead.billHeadShortForm) < 0;
    }
    

    Template:

    ng-options="... | filter: filterBillHead"
    

    See updated fiddle