AngularJS filter comparator true while displaying ng-repeat list when input field is empty

前端 未结 1 541
有刺的猬
有刺的猬 2021-01-03 05:41

I\'m going off by this example fiddle where it demonstrates the use of comparator parameter to filter exact matches....:

http://jsfiddle.net/api/post/library/pure/

相关标签:
1条回答
  • 2021-01-03 06:01

    You'll want to use a custom comparator function. It will allow you to perform a strict comparison except when the predicate is falsy.

    Your markup would then be:

     <input type="text" class="form-control" id="search.priority"
      title='Priority number to filter by'
      ng-model="search.priority" >
    
    <tr ng-repeat="workflowItem in workflows | filter:search:exceptEmptyComparator">
     <td>{{workflowItem.priority}}</td>
    

    And define the comparator function on your controller:

    $scope.exceptEmptyComparator = function (actual, expected) {
        if (!expected) {
           return true;
        }
        return angular.equals(expected, actual);
    }
    

    That should do the trick.

    0 讨论(0)
提交回复
热议问题